swift2

Call function when if value in NSUserDefaults.standardUserDefaults() changes

不羁的心 提交于 2019-12-22 10:03:29
问题 I have a variable that become value from NSUserDefaults.standardUserDefaults() var GiftCount = NSUserDefaults.standardUserDefaults().valueForKey("Gift") as! Int And i have a function named setGiftCount()... I need call this function when variable GiftCount has changed... How to do it? 回答1: First NSUserDefaults.standardUserDefaults().addObserver(self, forKeyPath: "Gift", options: NSKeyValueObservingOptions.New, context: nil) Second deinit { NSUserDefaults.standardUserDefaults().removeObserver

How can I make a function complete before calling others in an IBAction?

半世苍凉 提交于 2019-12-22 09:48:01
问题 I'm having trouble understanding completion handlers. I have a textFieldEditingDidChange IBAction that calls first a verify() function on the textfield input and then an if statement on the boolean returned by apply. The problem is that the if statement starts before verify() has finished . Here is the code: @IBOutlet weak var myTextField: UITextField! @IBAction func myTextFieldEditingDidChange(sender: AnyObject) { let yo = verify(myTextField.text!) print("\(yo)") // it always prints "true"

supportedInterfaceOrientationsForWindow in Swift 2.0

筅森魡賤 提交于 2019-12-22 09:37:02
问题 func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask { return UIInterfaceOrientationMask.Portrait.rawValue.hashValue | UIInterfaceOrientationMask.PortraitUpsideDown.rawValue.hashValue } That used to work in Swift 1.2 however the return line is now throwing this error: Binary operator '|' cannot be applied to two 'UIInterfaceOrientationMask' operands I am able to get to work with just 1 return value with the new

How to initialize CBCentralManager in Swift when a self reference is necessary

ε祈祈猫儿з 提交于 2019-12-22 09:00:10
问题 What are good ways to initialize an instance of CBCentralManager, which requires a delegate and is often pointing to the owning class? I could declare the property as an implicitly unwrapped optional but doing so as a general practice seems rather not Swift-like and not very safe. Alternatively, I can declare the property as an optional. But since CBCentralManager's initializers are not declared as failable, it doesn't seem to make sense to declare the instance as such. Implicitly Unwrapped

Searching TableView can't select row

…衆ロ難τιáo~ 提交于 2019-12-22 08:09:10
问题 While searching a tableView, every time I try to select a row it just takes me back to the unsearched tableView. What am I missing? the segue works perfectly when not filtering through the table. The ability to select a row just disapears while the searchBar is activated. import UIKit import Foundation class BenchmarkWODViewController: UITableViewController, UISearchResultsUpdating { var WodList = [WOD]() var FilteredWodList = [WOD]() var Keyword = "" var searchController : UISearchController

How to print on stderr with Swift?

无人久伴 提交于 2019-12-22 08:05:35
问题 I'm using Swift 2.2 on Linux and I need to write some debug output on the standard error stream. Currently, I'm doing the following: import Foundation public struct StderrOutputStream: OutputStreamType { public mutating func write(string: String) { fputs(string, stderr) } } public var errStream = StderrOutputStream() debugPrint("Debug messages...", toStream: &errStream) However, I've upgraded Swift to 2.2.1 but it seems that Foundation is no longer available. How to write on the standard

swift: async task + completion

删除回忆录丶 提交于 2019-12-22 07:04:59
问题 I got quite a lot of confusion on async tasks in swift. What i want to do is something like this... func buttonPressed(button: UIButton) { // display an "animation" tell the user that it is calculating (do not want to freeze the screen // do some calculations (take very long time) at the background // the calculations result are needed to update the UI } I tried to do something like this: func buttonPressed(button: UIButton) { let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY

Alamofire Swift 2 - cannot submit for beta testing (Xcode 7 GM)

假装没事ソ 提交于 2019-12-22 07:04:48
问题 I have followed the instructions on installing the swift2 branch of Alamofire with cocoa pods. The app works well in the simulator, however, when I archive it for submission with Xcode 7 GM I receive an email with an error: "The bundle contains an invalid implementation of Swift. The app may have been built or signed with non-compliant or pre-release tools" This issue definitely relates to the framework as without it I am able to submit for beta testing with the same version of Xcode. I have

Swift - associated types in protocol with where clause?

放肆的年华 提交于 2019-12-22 05:37:26
问题 Consider the following: protocol SomeProtocol { typealias F: Foo typealias FB: FooBar where FB.Foo == F } But this doesn't compile since we cannot put where clause in typealias like that. I must be missing something here since this can be easily done with type parameterization like this: struct SomeStruct<F: Foo, FB: FooBar where FB.Foo == F> {} What's the where clause equivalent for associated type ? 回答1: Type parameterization of associated types in protocols is currently not supported in

Change UITabBar selectedItem in Swift

风格不统一 提交于 2019-12-22 04:33:21
问题 How can I programmatically change the selected item in a UITabBar? 回答1: Swift 3 and later As of Swift 3, you can also use tabBarController.selectedIndex = 0 // (or any other existing index) (Thank you, @nidomiro.) Swift 2.2 and earlier Try the following tabBar.selectedItem = tabBar.items![newIndex] as! UITabBarItem Assuming you have access to the UITabBarController that owns the UITabBar , you can do the following self.selectedViewController = self.viewControllers![newIndex] as!