swift3

UIControlState.Normal is Unavailable

二次信任 提交于 2019-12-17 16:27:30
问题 Previously for UIButton instances, you were able to pass in UIControlState.Normal for setTitle or setImage . .Normal is no longer available, what should I use instead? let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 20, height: 20)) btn.setTitle("title", for: .Normal) // does not compile (This is a canonical Q&A pair to prevent the flood of duplicate questions related to this UIButton and UIControl changes with iOS 10 and Swift 3) 回答1: Swift 3 update: It appears that Xcode 8/Swift 3

Autoresizing issue in Xcode 8

∥☆過路亽.° 提交于 2019-12-17 15:54:13
问题 === EDIT ======== This issue is now solved in Xcode 8.1 . I have checked. ================ I don't know auto-layout properly. So, I am using autoresizing option in my all apps and it's fine for me. I am able to fulfill my all requirements by this without any issue. Now in Xcode 8, I have migrated my old swift project to swift 3. Now issue arises. See in image, I have set Autoresizing, and its working fine without issue in all devices until now and this project is live in AppStore, so that I

Swift 3 ObjC Optional Protocol Method Not Called in Subclass

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 15:38:29
问题 I have the following class hierarchy: class ScrollableViewController: UIViewController, UITableViewDelegate { // ... } That implements one UITableViewDelegate protocol method, e.g. tableView:willDisplayCellAt: In my class SpecificScrollableViewController , which inherits from ScrollableViewController , new optional protocol methods don't get called any more, e.g. tableView(_:heightForRowAt:) 回答1: tl;dr you need to prefix the function declaration with its Objective-C declaration, e.g. @objc

Check if location services are enabled

有些话、适合烂在心里 提交于 2019-12-17 15:21:41
问题 I've been doing some research about CoreLocation. Recently, I encountered a problem that has been covered elsewhere, but in Objective C, and for iOS 8. I feel kinda silly asking this, but how can you check if location services are enabled using swift, on iOS 9? On iOS 7 (and maybe 8?) you could use locationServicesEnabled() , but that doesn't appear to be working when compiling for iOS 9. So how would I accomplish this? Thanks! 回答1: Add the CLLocationManagerDelegate to your class inheritance

XMLParser.sharedParser.decode() in swift3

限于喜欢 提交于 2019-12-17 14:23:14
问题 I used this code XMLParser.sharedParser.decode() in swift2. But I migrate swift3 this code didn't work. My code is here, func xmppStream(_ sender: XMPPStream!, didReceive message: XMPPMessage!) { let result1 = XMLParser.sharedParser.decode(String(message)) for (key, value) in result1 { let title = result1["title"] } if message.isChatMessage(){ sms = String(message.body()) if( sms == "" || message.body() == nil){ }else{ self.prefs.set(sms, forKey: "SMS") } } } 回答1: If you are using codes from

In Swift, what's the difference between calling UINavigationController() vs UINavigationController.init()?

流过昼夜 提交于 2019-12-17 14:22:40
问题 In Swift, what's the difference between calling UINavigation() vs UINavigation.init() ? They both seem to return valid instance of UINavigationController . 回答1: UINavigationController() and UINavigationController.init() are the exact same thing. You can verify this by typing both into a Playground and then option -clicking on them. Both bring up the documentation for the same initializer. The Swift convention is to use just the type name (without .init ). 回答2: For some given type (e.g.

Quitting app causes error “Message from debugger: Terminated due to signal 9”

末鹿安然 提交于 2019-12-17 11:28:30
问题 I'm writing a basic music player app but having some problems when it comes to handling the app state transitions. I'm using Swift 3 and MPMusicPlayerController.systemMusicPlayer() The goal is this: 1) Keep music playing when user taps the Home button and app enters bg (works) 2) Stop the player ( myMP.stop() ) if the user the quits the app (works sometimes, throws error other times) I traced the flows using print statements based on possible actions and got this: Flow 2 is what I would

Why do Self and self sometimes refer to different types in static functions?

China☆狼群 提交于 2019-12-17 11:07:09
问题 Recently I have been developing multiple heavily protocol-oriented application frameworks with Swift and noticed a few (seemingly) odd behaviors with static functions in protocol extensions, specifically where the extension functions are invoked from metatypes. The way I initially discovered these behaviors was in troubleshooting a bug where the type of an object changed in a seemingly impossible way. I traced the problem down and eventually determined that it is because in a static function,

Convert NSDate to String in iOS Swift [duplicate]

坚强是说给别人听的谎言 提交于 2019-12-17 10:47:48
问题 This question already has answers here : Convert NSDate to NSString (14 answers) Closed last year . I am trying to convert a NSDate to a String and then Change Format. But when I pass NSDate to String it is producing whitespace. let formatter = DateFormatter() let myString = (String(describing: date)) formatter.dateFormat = "yyyy-MM-dd HH:mm:ss" let yourDate: Date? = formatter.date(from: myString) formatter.dateFormat = "dd-MMM-yyyy" print(yourDate) 回答1: you get the detail information from

UITableViewCell Buttons with action

青春壹個敷衍的年華 提交于 2019-12-17 10:33:15
问题 Hi I have a custom UITableViewCell with three buttons to handle a shopping cart function, Plus,Minus and Delete button and I need to know which cell has been touched. I've already tried to use the "tag solution" but it isn't working due to the lifecycle of the cells. Can anyone please help me find a solution? Thanks in advance 回答1: I was resolving this using a cell delegate method within UITableViewCell's subclass. Quick overview: 1) Create a protocol protocol YourCellDelegate : class { func