weird swift error after upgrading from 1.1 to 1.2 - ios

My project prior to the update was working fine with swift 1.1 and Xcode 6.1.1. i just upgraded to swift 1.2 and get a weird error in my AppDelegate
func application(application:UIApplication, didReceiveRemoteNotification userInfo:NSDictionary)
and also in a class derived from UITextFieldDelegate
func textFieldDidBeginEditing(textField: UITextField!) -> Bool // called textfield for locationField clicked
saying something along the lines
Objective-C method 'application:didReceiveRemoteNotification:'
provided by method 'application(:didReceiveRemoteNotification:)'
conflicts with optional requirement method
'application(:didReceiveRemoteNotification:)' in protocol
'UIApplicationDelegate'
the error for the textFieldDidBeginEditing is the same:
Objective-C method 'textFieldDidBeginEditing:' provided by method
'textFieldDidBeginEditing' conflicts with optional requirement method
'textFieldDidBeginEditing' in protocol 'UITextFieldDelegate'
What is the issue?

Just rewrite the method's name (should fix itself), and copy/paste the code in the methods. This should work.

This worked for me:
Before:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool
After:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool!

Related

UIApplicationLaunchOptionsKey not found

UIApplicationDelegate method - application(_:didFinishLaunchingWithOptions:) showing an error with Swift 4.2 (Xcode 10).
UIApplicationLaunchOptionsKey not found
What is replacement of UIApplicationLaunchOptionsKey in Swift 4.2?
'UIApplicationLaunchOptionsKey' has been renamed to 'UIApplication.LaunchOptionsKey'. Replace 'UIApplicationLaunchOptionsKey' with 'UIApplication.LaunchOptionsKey'.
Click on error hint, will show you solution:
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
}
Xcode would fix-it but UIApplicationLaunchOptionsKey is replaced by a nested type UIApplication.LaunchOptionsKey.
It should be UIApplication.LaunchOptionsKey, Please find following apple documentation
I have tried the below snippet of the code and worked for me.
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication: Any]? = nil) -> Bool {
}
Just provide UIApplication in launchOptions.
Hopes it will work for you as well. :)

Private func didFinishLaunchingWithOptions not being called? (Swift 3)

Isn't didFinishLaunchingWithOptions supposed to be called when the app starts running for the first time? I set a breakpoint at this method and when I run the app in the simulator the breakpoint doesn't get hit, which means the method doesn't get called. I'm trying to load some data from UserDefaults whenever the app launches, but it's being completely ignored. One thing I noticed is that it's by default a private func instead of a func. If I get rid of the private, I receive a warning that "there's an almost similar optional requirement in the UIApplicationDelegate". Can someone explain to me what this means and whether or not the private func has anything to do with the method being ignored? Is that method even supposed to be called when I run my app in the simulator? If not, how can I test if data is being retrieved after my app launches? All the other methods in the AppDelegate do get called normally (for example, the applicationDidEnterBackground method works perfectly fine).
Remove your method signature and have Xcode autocomplete it
I also had the problem that my didFinishLaunchingWithOptions method in AppDelegate would not be called. My function was also marked private and looked like this
private func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
The problem is that this is the old syntax! Apparently for me when I converted my project from Swift 2.x to Swift 3 Xcode did not convert the methods in AppDelegate. The new syntax looks like this
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool
Swift 4.2:
func application( _ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
for Swift ~3.0 Replace didFinishLaunchingWithOptions with
follwing signature
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
}
Have you implemented the didFinishLaunchingWithOptions in one of your ViewControllers? One will not get a call to the custom implementation of this method. This method is defined in the ApplicationDelegate and it will always be called once the app is launched. If you haven't defined the method again in any ViewController and the one in AppDelegate is not being called, then try resetting the simulator. From the simulator menu Simulator -> Reset content and settings.
If compiler prompts to make the didFinishLaunchingWithOptions method private then the parameter of the method might be causing the error.
The parameter of the application(_:didFinishLaunchingWithOptions:) delegate method is now bridged to Swift as a [UIApplicationLaunchOptionsKey: Any]?, rather than an [NSObject : AnyObject]?. So modify the method signature as shown.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// ...
}
This method is defined in the ApplicationDelegate and it will always be called once the app is launched. If you have not defined the method again in any ViewController and the one in AppDelegate is not being called, then try resetting the simulator.
Open simulator - > menu Simulator -> Reset content and settings.
-(BOOL)application(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//..
}
Update for Swift 4.2:
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool
Delete app from device and restart Xcode worked for me here

application(_:didFinishLaunchingWithOptions:)' nearly matches optional requirement

After installing Xcode 8 beta 6, I'm getting a warning saying:
Instance method 'application(_:didFinishLaunchingWithOptions:)' nearly matches optional requirement 'application(_:didFinishLaunchingWithOptions:)' of protocol 'UIApplicationDelegate'
in my App Delegate.
There are 2 suggested fixits to silence the warning:
Mark the method as private
Add #nonobjc to the method
Doing either silences the warning. But why does this need to be done?
iOS 12 SDK Update
In the iOS 12 SDK (that ships with Xcode 10), UIApplicationLaunchOptionsKey has now been renamed to the nested type UIApplication.LaunchOptionsKey, so you'll want:
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
// ...
}
iOS 10 and 11 SDKs (Xcode 8 and 9)
This warning is due to the fact that the didFinishLaunchingWithOptions: parameter of the application(_:didFinishLaunchingWithOptions:) delegate method is now bridged to Swift as a [UIApplicationLaunchOptionsKey: Any]?, rather than an [NSObject : AnyObject]?.
Therefore you'll need to update your implementation to reflect this change:
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?
) -> Bool {
// ...
}
Note that neither of Xcode's suggested fixes will actually fix the problem, they'll only conceal your implementation of application(_:didFinishLaunchingWithOptions:) from Objective-C – meaning that it'll never actually get called.
the first parameter passed into the function no longer has an external name. This is really just a minor detail since you don’t call this method directly, and it’s a quick fix to make the compiler happy. You can either manually edit that first parameter name to _, or just let Xcode handle this for you.
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool
or the New Syntax
func application(_ application:UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool // or remove = nil and try
you can get the latest Documentation from apple and sample link in here

Objective-C method conflicts with optional requirement method Swift

After the Xcode update, the compiler began to throw an error on the working code (both functions are in the AppDelegate.swift).
func application(application: UIApplication,
didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool
{
FBLoginView.self
FBProfilePictureView.self
return true
}
With error:
/Users/../AppDelegate.swift:14:11: Objective-C method 'application:didFinishLaunchingWithOptions:' provided by method 'application(:didFinishLaunchingWithOptions:)' conflicts with optional requirement method 'application(:didFinishLaunchingWithOptions:)' in protocol 'UIApplicationDelegate'
And second
func application(application: UIApplication,
openURL url: NSURL,
sourceApplication: NSString?,
annotation: AnyObject) -> Bool {
var wasHandled:Bool = FBAppCall.handleOpenURL(url, sourceApplication: sourceApplication as! String)
return wasHandled
}
with error
/Users/../AppDelegate.swift:25:11: Objective-C method
'application:openURL:sourceApplication:annotation:' provided by method 'application(:openURL:sourceApplication:annotation:)' conflicts with optional requirement method 'application(:openURL:sourceApplication:annotation:)' in protocol 'UIApplicationDelegate'
I understand that most likely I should like you to stick together somehow these two functions into one. I do not understand why this code suddenly stopped working in 6.3, despite the fact that it worked in 6.2.
I'm not sure exactly why the compiler is throwing the error, however I do see a difference in the default Swift version of those same methods. Perhaps you could replace the function declaration with those created with a normal Swift project:
1
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
2
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool
I'd recommend replacing your method declarations with the above to see if it compiles now.
EDIT 1 (9/21/2015): I've confirmed these are now up to date for Xcode 7's public release. They removed the optional (annotation: AnyObject?) and made it (annotation: AnyObject), in declaration #2.
The type of the launchOptions parameter of the didFinishLaunchingWithOptions function was changed in XCode 6.3:
"launchOptions: NSDictionary?" has become "launchOptions: [NSObject: AnyObject]?"
Just change your function header to match the following:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
You should also make sure you are using the correct type. Use String instead of NSString.
Try overriding that method again from Xcode completions. Worked for me.
launchOptions have been changed; try changing out "launchOptions: NSDictionary?" to "launchOptions: [NSObject: AnyObject]?"
Hope this helps!

cocos2d CCAppDelegate issue with swift and didFinishLaunchingWithOptions

Is anyone else having issues running a simple Cocos2d v3.1 on Swift with the xcode 6 beta 5?
#UIApplicationMain class AppDelegate : CCAppDelegate, UIApplicationDelegate {
override func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool
{
setupCocos2dWithOptions([CCSetupShowDebugStats: true])
return true
}
override func startScene() -> (CCScene)
{
return HelloWorldScene()
}
I'm having the issue with my own project, but I found a sample project on github with the same issue:
https://github.com/chunkyguy/Cocos2dSwift
The didFinishLaunchingWithOptions function has the error:
Overriding method with selector 'application:didFinishLaunchingWithOptions:' has incompatible type '(UIApplication!, NSDictionary!) -> Bool'
Changing the function signature to:
override func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]) -> Bool
fixes the compiler errors but the app crashes with EXC_BAD_ACCESS on the AppDelegate.
Has anyone come across this issue or can suggest a fix?
So I wasn't clearly reading the error message in detail. I was missing out exclamation marks because I copied and pasted the UIApplicationDelegate swift generated headers. I should have entered:
override func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]!) -> Bool
Command clicking on UIApplication delegate does show:
protocol UIApplicationDelegate : NSObjectProtocol {
...
optional func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]) -> Bool
i.e. without the '!'s

Resources