swift2

Understanding Swift 2.2 Selector Syntax - #selector()

巧了我就是萌 提交于 2019-12-17 17:39:22
问题 I am switching over the syntax of my project toward Swift 2.2 (which xCode helps me do automatically); however, I do not understand the new #selector() syntax. As an example: timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(MyVC.timerCalled(_:)), //new selector syntax! userInfo: nil, repeats: true) This has the selector #selector(MyVC.timerCalled(_:)) What does the _: signify? Can you add other variables into this selector? Say, #MyVC.timerCalled(_

Swift - “use of unresolved identifier”

时间秒杀一切 提交于 2019-12-17 16:58:27
问题 I am studying Swift! I want create Main interface on xib. But have error "use of unresolved identifier". Add code from MainViewController import Foundation public class MainViewController : BaseViewController { override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) // TODO: Write your test code here // ... } } UPD:(Add image) 回答1: 1) Right Click your MainViewController.swift 2) Select Show File Inspector 3) On Right side panel, look at Target Membership 4) Add your

How to restrict certain characters in UITextField in Swift?

最后都变了- 提交于 2019-12-17 16:50:21
问题 I am creating a trivia application that asks for a username on start up. I'd like to make it impossible to use characters such as #$@!^& etc (also including "space"). I took a look at this post here but it is written entirely in Objective-C. Thanks in advance. 回答1: Since you're explicitly asking for Swift, I've translated the top asnwer in the linked question. let notAllowedCharacters = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_."; func textField( textField: UITextField

perform segue with identifier wont work in swift 2

徘徊边缘 提交于 2019-12-17 16:48:07
问题 I have been using this code to perform a custom segue when a user logs in to the application: dispatch_async(dispatch_get_main_queue()){ self.performSegueWithIdentifier("showSTPS", sender: self) } I currently have this code in my perpareForSegue (im not totally sure if i need it) override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){ if "showSTPS" == segue.identifier { } } And I am getting the following error every time I try to perform the segue: 2015-08-31 11:56:28.998

What causes 'Constant captured by a closure before being initialized' error

冷暖自知 提交于 2019-12-17 16:47:05
问题 In a following class class Foo { let _defaultValue = "N/A" let value: String init (dict: NSDictionary) { self.value = dict["bar"] as? String! ?? _defaultValue } } compiler fails with the message constant 'self.value' captured by a closure before being initialized As far as I can see, no operators read `self.value. The message seems somewhat confusing. I accidentally came up with a workaround. I should say it confuses me even more: class Foo { let value: String init (dict: NSDictionary) { let

What's wrong here: Instance member cannot be used on type [duplicate]

旧巷老猫 提交于 2019-12-17 16:25:23
问题 This question already has answers here : How to initialize properties that depend on each other (4 answers) Closed 4 years ago . I have the following code and I'm confused about this error message: Instance member 'mydate' cannot be used on type 'TableViewController' Code: class TableViewController: UITableViewController { let mydate = NSDate() let items = [ (1, 9, 7, "A", mydate), (2, 9, 7, "B", mydate), (3, 9, 7, "C", mydate), (4, 9, 7, "D", mydate) ] When I write the following, I can build

How to list all classes conforming to protocol in Swift?

折月煮酒 提交于 2019-12-17 15:58:06
问题 How to list all classes implementing a given protocol in Swift? Say we have an example: protocol Animal { func speak() } class Cat:Animal { func speak() { print("meow") } } class Dog: Animal { func speak() { print("Av Av!") } } class Horse: Animal { func speak() { print("Hurrrr") } } Here is my current (not compilable) approach: func getClassesImplementingProtocol(p: Protocol) -> [AnyClass] { let classes = objc_getClassList() var ret = [AnyClass]() for cls in classes { if class

extension of Dictionary where <String, AnyObject>

倖福魔咒の 提交于 2019-12-17 15:47:12
问题 I am trying to create a dictionary extension where Dictionary is of the type <String, AnyObject>. Was looking in many places and trying different approaches, but none of them seemed to work. This was one of them: extension Dictionary where <String, AnyObject>{ var jsonString:String { return "" } } Another method that didn't actually work for some reason: extension Dictionary where Key:Hashable, Value:AnyObject { var jsonString:String { do { let stringData = try NSJSONSerialization

Swift 2 / iOS 9 - libz.dylib not found

余生长醉 提交于 2019-12-17 15:16:34
问题 I'm using some external codes from google in my new Swift 2.0 project that required "libz.dylib" in earlier versions. After updating to the new XCode / the new SDK. XCode is now unable to import the libz.dylib and throws some errors Undefined symbols for architecture i386: "_deflate", referenced from: +[GAICompressionUtil gai_dataByCompressingBytes:length:compressionLevel:mode:] in libGoogleAnalyticsServices.a(GAICompressionUtil.o) "_deflateEnd", referenced from: +[GAICompressionUtil gai

How to silence a warning in swift

大憨熊 提交于 2019-12-17 15:08:34
问题 I have a piece of code which is generating lots of warnings (deprecated API) Using clang* I could do #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" ... #pragma clang diagnostic pop However this does not work in swift. How to do it in swift ? Note: I don't want to disable the warning globally, nor even file wide, but just disable a specific warning in a specific part of my source code. Edit: I looks like my note was not clear enough: I do NOT want