我试图使用泛型来简化一些XML反序列化,但是,Swift 2.0令人窒息地说我没有在方法签名中使用泛型。因为我直接实例化了传入的类类型,所以我对为什么它引发此错误感到困惑。关于此错误原因的任何建议吗?确切的错误是:
函数签名中未使用通用参数“ T”
MTXMLDeserializable是我正在使用的基类,并具有以下方法: init(properties: Dictionary<String, String>?, propertyMap: Dictionary<String, String>?)
这是令人反感的方法:
func transformResponse<T: MTXMLDeserializable>(responseData: XMLIndexer?) -> [MTXMLDeserializable]? {
if let data = responseData {
let properties = data["body"].children
return properties.map {
(xmlProps) -> MTXMLDeserializable in
let objProps = xmlProps.element!.attributes
return (T.self as T.Type).init(properties: objProps, propertyMap: self.propertyMap)
}
} else {
return .None
}
Run Code Online (Sandbox Code Playgroud)
认为使用传递给properties.map我的闭包中的泛型内部可能会令人窒息,我将方法重写为:
func transformResponse<T: MTXMLDeserializable>(responseData: XMLIndexer?) -> [MTXMLDeserializable]? {
if let data = responseData {
let properties = data["body"].children
let objs = NSMutableArray()
for xmlProps …Run Code Online (Sandbox Code Playgroud) 我目前正在安排本地通知,如果用户当天尚未打开应用程序,则每天下午6点出现一次.如果用户已经加载了应用程序,那么我想取消当天的通知,并在明天下午6点安排新的通知.但是,当我尝试迭代计划通知列表(这不是我在应用程序中的唯一本地通知)时,通知会正确显示,[[UIApplication sharedApplication] scheduledLocalNotifications]数组始终为空.以下是给我带来麻烦的代码段:
// See if we need to create a local notification to tell the user that they can receive a daily reward
NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications]; // <- This is always empty
// Iterate over all the pending notifications
for( int iter = 0; iter < [notifications count]; iter++ )
{
UILocalNotification* localNotification = [notifications objectAtIndex:iter];
if( [[localNotification.userInfo objectForKey:@"source"] isEqualToString:@"dailyReminder"] )
[[UIApplication sharedApplication] cancelLocalNotification:localNotification];
}
NSCalendar* calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
[calendar setTimeZone:[NSTimeZone localTimeZone]];
NSDateComponents …Run Code Online (Sandbox Code Playgroud) iphone objective-c ios localnotification uilocalnotification