swift2.2

How to invoke a class method using performSelector() on AnyClass in Swift?

☆樱花仙子☆ 提交于 2020-07-06 10:41:40
问题 In ObjC you could simply invoke a class method using the class method from NSObject . [Machine performSelector:@selector(calculate:) withObject:num]; But how do you do this in Swift 2.2? @objc(Machine) // put it here, so you can simply copy/paste into Playground class Machine: NSObject { static func calculate(param: NSNumber) -> String { if param.integerValue > 5 { return "42" } return "42" // there is only 1 answer to all the questions :D } } if let aClass = NSClassFromString("Machine") {

How to invoke a class method using performSelector() on AnyClass in Swift?

北慕城南 提交于 2020-07-06 10:41:26
问题 In ObjC you could simply invoke a class method using the class method from NSObject . [Machine performSelector:@selector(calculate:) withObject:num]; But how do you do this in Swift 2.2? @objc(Machine) // put it here, so you can simply copy/paste into Playground class Machine: NSObject { static func calculate(param: NSNumber) -> String { if param.integerValue > 5 { return "42" } return "42" // there is only 1 answer to all the questions :D } } if let aClass = NSClassFromString("Machine") {

What are '!' and '?' marks used for in Swift

人走茶凉 提交于 2019-12-24 13:35:43
问题 When I changed from Objective-C to Swift programming, I have come across ' ! ' (exclamation mark) and ' ? ' (question mark) that often have to be put right after a property , a method call etc. What are these marks used for, and what happens if we did not use them? I searched for a thorough answer on the web but could not find any. So I decided to put here my answer in case for anyone is in search of a clear answer for that. I also included in my answer below a link to a question that is

Can't get UITextField to autoshrink the font

一笑奈何 提交于 2019-12-23 05:18:02
问题 In Swift, i have a UITextField on a table view cell, and when it's text becomes too long I would like the font size to decrease. I want to make it very clear that I am talking about a UITextField, not a UILabel or a UITextView. The reason I say this is because I have seen this question pop up several times and the answers were all based on UILabel instead of UITextField. I hoped, that can be done in IB, where i did this settinngs of Min Font Size and ajust to fit, but this didn´t change

No Method declared with Objective-C Selector for Notification UIKeyboardWillShowNotification and UIKeyboardWillHideNotification

寵の児 提交于 2019-12-19 19:45:09
问题 After the recent update of Xcode, this code that used to work no longer works. Most of the Selector(":") has an auto correction with the exception for this code: override func viewDidLoad() { super.viewDidLoad() NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil); NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification,

Swift 2.2 breaks optionals/unwrapping optionals

若如初见. 提交于 2019-12-13 13:32:12
问题 Swift 2.2 has broken almost all my code. Even this simple string assigning to label doesn't work anymore: cell.categoryName.text = peopleArray![indexPath.row]["Name"] as? String The error says " Downcast from 'String?!' to 'String' only unwraps optionals, did you mean to use '!!'? " What changes do I have to do now. EDIT : More Problems: if (dict["data"]!["DataDetails"] as! NSArray).count == 0 { } Due to this I am getting a segmentation fault and the error shows this: warning: cast from

viewForHeaderInSection incorrect width

佐手、 提交于 2019-12-12 03:28:11
问题 Width of my headerView does not adapt to the width of the screen. My viewForHeaderInSection function: func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let cell = self.tableView.dequeueReusableCellWithIdentifier("sectionCell") as! sectionCell cell.hint.text = "some name" cell.tag = section let singleTap = UITapGestureRecognizer(target: self, action: #selector(singleTapOnSection(_:))) cell.addGestureRecognizer(singleTap) let view = UIView(frame: cell

How to separate attributes from a NSAttributed String and apply these attributes on other string?

↘锁芯ラ 提交于 2019-12-11 01:02:49
问题 eg. Like we have a NSAttributed string and we need to separate string and attributes, then use these attributes on other string of same length. 回答1: An NSAttributedString may have different attributes for different ranges of the string. To extract these attributes, you can use the enumerateAttributesInRange method. We prepare an array of tuples to hold the results: var extractedAttributes = [(attributes: [String:AnyObject], range: NSRange)]() Each tuple will hold the attributes for a specific

Using String.CharacterView.Index.successor() in for statements

血红的双手。 提交于 2019-12-10 16:09:23
问题 In the future, C-style for statements will be removed from Swift. While there are many alternate methods to using the C-style for statements, such as using stride , or the ..< operator, these only work in some conditions For example, in older versions of swift, it was possible to loop through every other one of the indexes, String.CharacterView.Index, of a String using C-style for statements for var index = string.startIndex; index < string.endIndex; index = string.successor().successor(){ /

Type of optionals cannot be inferred correctly in swift 2.2

别说谁变了你拦得住时间么 提交于 2019-12-09 03:32:10
问题 Problem: When running the following code under Xcode 7.3 with swift 2.2, the compiler is unable to correctly infer the type of the optional: import Foundation func whatAmI<T>(inout property:T?) { switch property { case is Int?: print("I am an Int?") case is String?: print("I am a String?") default: print("I don't know what I am") } } var string : String? whatAmI(&string) On my side with Xcode 7.3 this will print I am an Int? However, when I initialize the variable with an empty string before