How do you use NSAttributedString?

When building attributed strings, I prefer to use the mutable subclass, just to keep things cleaner. That being said, here’s how you create a tri-color attributed string: NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@”firstsecondthird”]; [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,5)]; [string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(5,6)]; [string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(11,5)]; typed in a browser. caveat implementor Obviously … Read more

How do I make an HTTP request in Swift?

You can use URL, URLRequest and URLSession or NSURLConnection as you’d normally do in Objective-C. Note that for iOS 7.0 and later, URLSession is preferred. Using URLSession Initialize a URL object and a URLSessionDataTask from URLSession. Then run the task with resume(). let url = URL(string: “http://www.stackoverflow.com”)! let task = URLSession.shared.dataTask(with: url) {(data, response, error) … Read more

Upload image with parameters in Swift

In your comment below, you inform us that you are using the $_FILES syntax to retrieve the files. That means that you want to create a multipart/form-data request. The process is basically: Specify a boundary for your multipart/form-data request. Specify a Content-Type of the request that specifies that it multipart/form-data and what the boundary is. … Read more

How to return value from Alamofire

As mattt points out, Alamofire is returning data asynchronously via a “completion handler” pattern, so you must do the same. You cannot just return the value immediately, but you instead want to change your method to not return anything, but instead use a completion handler closure pattern. Nowadays, that might look like: func getOrders(completionHandler: @escaping … Read more