Swift 3:
w Swift 3 launchOptions
jest słownikiem typu [UIApplicationLaunchOptionsKey: Any]?
, więc chcesz uzyskać dostęp do wartości takiego:
launchOptions?[UIApplicationLaunchOptionsKey.url]
Ponieważ klucz typ jest UIApplicationLaunchOptionsKey
, ty można skrócić typ enum
jako po prostu .url
:
launchOptions?[.url]
Wartość skojarzona z tym kluczem to jednak URL
, a nie jako String
. Klucz może też nie być obecny w słowniku, więc zamiast normalnego przesyłania używaj funkcji odlewania warunkowego as?
.
w Swift, chcesz zrobić:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if let url = launchOptions?[.url] as? URL {
// If we get here, we know launchOptions is not nil, we know
// key .url was in the launchOptions dictionary, and we know
// that the type of the launchOptions was correctly identified
// as URL. At this point, url has the type URL and is ready to use.
}
Swift 2:
W kodzie launchOptions
jest słownikiem typu [NSObject: AnyObject]?
, tak że chcesz uzyskać dostęp wartość taka jak ta:
options?[UIApplicationLaunchOptionsURLKey]
Wartość skojarzona z tym kluczem to jednak NSURL
, a nie String
. Klucz może też nie być obecny w słowniku, więc zamiast normalnego przesyłania używaj funkcji odlewania warunkowego as?
.
w Swift, chcesz zrobić:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
if let url = launchOptions?[UIApplicationLaunchOptionsURLKey] as? NSURL {
// If we get here, we know launchOptions is not nil, we know
// UIApplicationLaunchOptionsURLKey was in the launchOptions
// dictionary, and we know that the type of the launchOptions
// was correctly identified as NSURL. At this point, url has
// the type NSURL and is ready to use.
}
Perfect! Dziękuję –
Więc nie trzeba robić: if (URLString.isEmpty!) { \t // zrobić kilka rzeczy } zgodnie z własnymi uwagami? –
Będziesz mieć obiekt 'NSURL'. Będziesz chciał uzyskać dostęp do jego części. Na przykład 'if let path = NSURL.path {// path is a String'. – vacawama