swift3

How to call non-escaping closure inside a local closure? [duplicate]

倖福魔咒の 提交于 2020-01-02 05:29:26
问题 This question already has answers here : Why do closures require an explicit `self` when they're all non-escaping by default in Swift 3? (2 answers) Closed 3 years ago . I have a function which looks something like this: func test(closure: () -> ()) { let localClosure = { closure() } localClosure() } This is only an example and does not fully reflect the problem I encountered, obviously here I could have just called closure directly! It should be clear that in the above code, closure cannot

How to disable UIAlertAction depending on UITextField in UIAlertController?

末鹿安然 提交于 2020-01-02 04:45:08
问题 I'm using a UIAlertController to present the user with a dialog to enter a 5 digit CRN . I want the Add button to be disabled until there are 5 and only 5 digit in the UITextField . Here's what the UI looks like : Here's the properties setup for the UIAlertController : var alertController: UIAlertController! var addAlertAction: UIAlertAction! { return UIAlertAction(title: "Add", style: .default) } Here's how I'm initializing them in the viewDidLoad method : self.alertController =

Swift 3: Why symbol _ is added before sender in parameters of action?

天大地大妈咪最大 提交于 2020-01-02 04:45:06
问题 My Xcode has been recently updated to Xcode 8 with Swift 3. What I have noticed is that when attaching actions to ViewController functions are generated in this style: @IBAction func methodName(_ sender: UIButton) { } I don't understand what is the purpose of having _ there. Could anyone explain why it is there? In previous Swift 2.2 it was not there. Another question is that previous project I have done by watching tutorial has migrated to Swift 3, and there was one method which looked like

Alamofire 4, Swift 3 and building a json body

对着背影说爱祢 提交于 2020-01-02 04:11:29
问题 {"title":"exampleTitle","hashTags":[{"name":"tag1"},{"name":"tag2"}],"uploadFiles": [{"fileBytes":"seriesOfBytes\n","filename":"upload.txt"}]} That is my desired body I want to send to the backend. I'm using Swift 3.0 and Alamofire 4 and i have multiple questions. first , How do i correctly create a body which contains values and arrays of values? My approach is: let para:NSMutableDictionary = NSMutableDictionary() para.setValue("exampleTitle", forKey: "title") let jsonData = try!

Swift 3 Load xib. NSBundle.mainBundle().loadNibNamed return Bool

北城以北 提交于 2020-01-02 02:28:06
问题 I was trying to figure out how to create a custom view using xib files. In this question the next method is used. NSBundle.mainBundle().loadNibNamed("CardView", owner: nil, options: nil)[0] as! UIView Cocoa has the same method,however, this method has changed in swift 3 to loadNibNamed(_:owner:topLevelObjects:), which returns Bool , and previous code generates "Type Bool has no subscript members" error, which is obvious, since the return type is Bool. So, my question is how to a load view

Minimal working IteratorProtocol / Sequence in Swift 3

人走茶凉 提交于 2020-01-02 02:01:09
问题 I've found it remarkably difficult to find "working documentation" for using Sequence / IteratorProtocol in Swift 3. The few tutorials/articles out there seem to be for older Swift. Imagine a toy doubly-linked list class called DLList ... public class Node { // whatever "thing" you have a group of, this is that "thing" } public class DLList { // toy linked list class here // so this is a group of "node" in this example } I believe the following represents the simplest (?), correct, way to

Error Domain=kAFAssistantErrorDomain Code=209 “(null)”

跟風遠走 提交于 2020-01-02 01:14:06
问题 I am facing an issue with the SFSpeechRecognizer . After few seconds of starting the application I start getting the error Error Domain=kAFAssistantErrorDomain Code=209 "(null)"" and "Error Domain=kAFAssistantErrorDomain Code=203 "Timeout" UserInfo={NSLocalizedDescription=Timeout, NSUnderlyingError=0x170049990 {Error Domain=SiriSpeechErrorDomain Code=100 "(null)"}} and the application stops recognization. I am trying to endAudio() and then again start it every 20 seconds as Apple does not

How to quickly localize storyboard on the fly?

狂风中的少年 提交于 2020-01-02 01:07:13
问题 I am trying to localize my app using Swift 3 and Xcode 8.1. I am trying to localze the app using both storyboard(main.strings(German)) and programatically using "NSLocalizedString" . I am able to localize programatically using "NSLocalizedString" using following code func localized(lang:String) ->String { let path = Bundle.main.path(forResource: lang, ofType: "lproj") let bundle = Bundle(path: path!) return NSLocalizedString(self, tableName: nil, bundle: bundle!, value: "", comment: "") } But

Save photo with geolocation data to photo library Swift 3

非 Y 不嫁゛ 提交于 2020-01-01 11:01:14
问题 How can I save a photo to photo library with geolocation metadata? I have requested (and allowed) the app to access user location: private func allowAccessToUserLocation() { locationManager = CLLocationManager() locationManager.delegate = self locationManager.requestWhenInUseAuthorization() } Do I need to ask for specific premissions for the camera app? EDIT: I use UIImagePickerController to take photos from within the app. All photos taken from within the app are stored in the photo library

Deprecating renamed method with multiple arguments

谁说胖子不能爱 提交于 2020-01-01 09:17:46
问题 I wanted to update the naming of the following method for Swift 3: public func imageWithUrl(url: String, placeholderNamed: String) { if let image = UIImage(named: placeholderNamed) { imageWithUrl(url: url, placeholder: image) } else { imageWithUrl(url: url) } } to public func image(url: String, placeholderNamed: String) { So I deprecated the old method with this: @available(*, deprecated: 1.8, renamed: "image(url:, placeholder:") The problem is that I'm getting the following error: 'renamed'