Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Swift function completion

Given your definition of call_api_function, you would call it like so:

call_api_function() { response in


    // now use `response` here

I'd suggest you do a little research on trailing closures in the The Swift Programming Language: Closures.

But, having said that, your call_api_function has problems of its own.

  • You're doing a forced unwrapping of the error optional. What if there was no error? Then, the forced unwrapping of the nil optional would fail and the code would crash.

  • If the request succeeded, you're not doing anything with the data that is returned. Presumably you did the request because you wanted to do something with the returned data.

    Unfortunately, you don't provide information about the nature of the XML response you're expecting, but presumably you would instantiate a NSXMLParser instance to parse it and then implement the NSXMLParserDelegate methods and call the parse method.

  • Following up on the prior points, rather than a closure with a single non-optional parameter, I'd expect to see a closure with two optional parameters, an optional with the parsed data (which would be set if the request and parsing was successful) and an optional with a NSError (which would be set only if there was an error).

  • A very minor point, but you might want to adopt a Cocoa naming conventions (e.g. camelCase convention of callApiFunction).

Categories : IOS

Related to : Swift function completion
Swift Random Function
Assuming I'm understanding your question correctly, you want this: func createObstacle() -> () -> () { var obstacles = [obstacle1, obstacle2, obstacle3] var randomObstacle = Int(arc4random_uniform(UInt32(obstacles.count))) return obstacles[randomObstacle] } Check out the documentation section about functions:

Categories : Function
swift function's default absence value
Yes, that will be valid code. With default value for 2nd parameter you indeed can provide only one parameter to initializer: Starship(name:"SuperHero") That will be identical to the following call: Starship(name:"SuperHero", prefix:nil)

Categories : Swift
Call function on app termination in Swift
There are a few methods in UIAppDelegate that will help you. Take a look at applicationWillTerminate(_:) and applicationWillResignActive(_:). From there you see what state your app is in and do perform the appropriate actions.

Categories : Swift
Swift - Select Random Function
func fireEvent() { var randomFunction = [addEnemyFromCornersTogether(), addEnemyFromCornersWait()] let choice = Int(arc4random_uniform(UInt32(randomFunction.count))) var choiceFunction: () = randomFunction[choice] let delay = 5.0 * Double(NSEC_PER_SEC) let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay)) dispatch_after(time, dispatch_get_main_queue(), { choiceF

Categories : Swift
Swift equal function dont work
Operators must be implemented in the global scope, not inside the class. So you should implement your equality operator outside of the class: class Test {...} func == (lhs: Test, rhs: Test) -> Bool { return lhs.key == rhs.key } Suggested reading: Operator Functions

Categories : Swift
Recently Add
keychain iOS touchId prompted twice
-[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7fc748e37ea0'
Search Results Visibly Scrolling Underneath the Status Bar
How do I exclude an IOS 8 app extension from a build?
EXC_BAD_ACCESS when capturing a GPU frame
iOS 8 - Map keeps reanimating back to user location
Constantly refreshing background image of UIImageView IOS
UINavigationController keeps displaying popped view
Swift closure syntax using Shark Food Mute Switch?
MagicalRecord 3.0 not saving. No objects for the second build
iOS / Swift Can't add gesture to child class
iOS: Is there currently a way to prevent two view controllers being pushed or popped at the same time?
Checking if array contains two objects
How should turning of SCNLight affect background color?
Why don't my views show up in Storyboards and are greyed out in the left pane?
SWIFT convert NSTimeInteval to NSDate
BLE scanning is stopped when go to background?
Drawing a Rect on UIView
HLS playback via local HTTP server on iOS
Re-Assigning SKScene delegate protocol
Network lost on screen lock unlock
Annoying Xcode Error "file not found" #import
CoreData not firing faults
iOS:How do you change a view size/origin from another view controller class?
Changing a frame in viewDidLayoutSubviews
Animate multiple shapes in UIView
Add NSIntegers ONLY when 2nd NSInteger Changes Value
Facebook SDK, fetching friends
How to get Storyboard to support dynamically sized cells in UICollectionView?
© Copyright 2017 Publishing Limited. All rights reserved.