Hey! I have been learning how to program swift because I want to write iOS apps.
For the first, I considered start with recognize my previous concepts in this new language for me, here is my “fizzbuzz” example:
for i in 1...50 { | |
if i%5==0 && i%3==0 { | |
print("Fizzbuzz 🍻 ") | |
} | |
else if i%5 == 0{ | |
print("Buzz") | |
} | |
else if i%3==0{ | |
print("Fizz") | |
} | |
else{ | |
print("\(i)") | |
} | |
} |
My guide to learn Swift and iOS are the follow videos and books:
- Swift Mastering The Core Concepts
- Building iOS 10 Applications with Swift
- iOS Programming The Big Nerd Ranch Guide 6Th Edition
My First App
First, I need to understand this:
- How to create a single view iOS app.
- How to use the story board.
- How to put a buttons and labels.
- How to connect the view and the controller.
- How to make an event (push a button)
With these learnings, I was be able to create a basic iOS app.
I created a simple view with many input text, labels and buttons. My first approach was make a println in console when pushed the button.
Finding a REST Library
I need find libraries for add to my iOS App, so I installed Cocoa Pods for this job.
I will use a library for make REST request, so I found Alamofire.
The init command produces a Podfile, where I add my dependency:
Sending a message to slack
Previously I wrote a post where I describe how to send messages to slack channel, you can read again here.
For send a post request with alamofire library, I need to write this:
func sendSlackMessage(message:String, emoji:String){ | |
let slackUrl = "http://your-slack-channel-url.com" | |
Alamofire.request(slackUrl, | |
method: .post, | |
parameters: | |
["channel": "#general", | |
"username": "Home Office Bot", | |
"text":message, "icon_emoji":emoji], | |
encoding: JSONEncoding.default) | |
.responseString{ response in | |
print(response) | |
} | |
} |
This was my first iOS app, although I have a lot of errors, I’m so excited because I can do a simple post request.
Thanks for reading!