2015-06-14 6 views
15

मैं क्या उत्पादन किया गया था पिछले नवंबर (here), लेकिन मेरे पुराने कोड Xcode 7 में किसी भी अधिक मेरे लिए काम नहीं लगता है परे registerUserNotificationSettings पर किसी भी दस्तावेज और स्विफ्ट 2.स्विफ्ट 2 में रजिस्टर यूज़र नोटिफिकेशन सेटिंग्स में परिवर्तन?

मेरे पास है खोजने के लिए नहीं कर पा रहे अनुप्रयोग प्रतिनिधि में इस कोड:

'Element.Protocol' एक सदस्य 'चेतावनी'

नामित नहीं है:

let endGameAction = UIMutableUserNotificationAction() 
endGameAction.identifier = "END_GAME" 
endGameAction.title = "End Game" 
endGameAction.activationMode = .Background 
endGameAction.authenticationRequired = false 
endGameAction.destructive = true 

let continueGameAction = UIMutableUserNotificationAction() 
continueGameAction.identifier = "CONTINUE_GAME" 
continueGameAction.title = "Continue" 
continueGameAction.activationMode = .Foreground 
continueGameAction.authenticationRequired = false 
continueGameAction.destructive = false 

let restartGameCategory = UIMutableUserNotificationCategory() 
restartGameCategory.identifier = "RESTART_CATEGORY" 
restartGameCategory.setActions([continueGameAction, endGameAction], forContext: .Default) 
restartGameCategory.setActions([endGameAction, continueGameAction], forContext: .Minimal) 

application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: (NSSet(array: [restartGameCategory])) as Set<NSObject>)) 

मैं अब कोड की अंतिम सीमा पर निम्नलिखित दो त्रुटियों का सामना करना

और

प्रकार का एक तर्क सूची के साथ 'registerUserNotificationSettings' '(UIUserNotificationSettings)' आह्वान नहीं किया जा सकता

मैं किसी भी परिवर्तन के बारे में जानकारी के लिए खोज की है, लेकिन मैं नहीं मिल सकता है कुछ भी। क्या मुझसे साफ़ - साफ़ कुछ चीज़ चूक रही है?

उत्तर

30
इसके बजाय तो जैसे (NSSet(array: [restartGameCategory])) as? Set<UIUserNotificationCategory>) साथ (NSSet(array: [restartGameCategory])) as Set<NSObject>) का उपयोग करने का

:

application.registerUserNotificationSettings(
    UIUserNotificationSettings(
     forTypes: [.Alert, .Badge, .Sound], 
     categories: (NSSet(array: [restartGameCategory])) as? Set<UIUserNotificationCategory>)) 
21

@ प्रतिबंध लगाने का जवाब काम करेंगे, लेकिन यह एक अधिक Swifty तरह से यह करने के लिए संभव है। NSSet और डाउन कास्टिंग का उपयोग करने के बजाय, आप इसे सामान्य प्रकार UIUserNotificationCategory के साथ सेट का उपयोग करके ग्राउंड अप से बना सकते हैं।

let categories = Set<UIUserNotificationCategory>(arrayLiteral: restartGameCategory) 
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: categories) 
application.registerUserNotificationSettings(settings) 

यह भी ध्यान देने योग्य है कि कई लाइनों पर कोड को तोड़ने से आपको यह पता चलने में मदद मिलेगी कि समस्या कहां है। इस मामले में, आपकी दूसरी त्रुटि केवल अभिव्यक्ति के बाद से पहले का परिणाम है।

और जैसा कि नीचे उनकी टिप्पणी में @stephencelis द्वारा विशेषज्ञ रूप से इंगित किया गया था, सेट ArrayLiteralConvertible हैं, इसलिए आप इसे निम्न तरीके से नीचे कम कर सकते हैं।

let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: [restartGameCategory]) 
+0

यह निश्चित रूप से मेरे मूल कोड की तुलना में बहुत साफ दिखता है - धन्यवाद, @ 0x7fffffff। –

संबंधित मुद्दे