【问题标题】:"unrecognized selector sent to instance" property setter“无法识别的选择器发送到实例”属性设置器
【发布时间】:2016-02-11 13:35:29
【问题描述】:

我有一个继承自 UILocalNotification 的自定义类,它有一个 Store 对象属性(继承自 NSObject 的类):

customLocalNotification.h

@property (strong,nonatomic) Store *store;

但是当我尝试像这样在viewController 中设置“Store”对象时:

customLocalNotification *notification=[[customLocalNotification alloc] init];
notification.store=store; //Crashes here

编辑:我将使用userInfo 字典来保存我的Store 对象,但是当我尝试这样做时,它会崩溃并显示以下错误消息:

2016-02-11 15:55:26.132 App[13861:3606796] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'unable to serialize userInfo: Error Domain=NSCocoaErrorDomain Code=3851 "Property list invalid for format: 200 (property lists cannot contain objects of type 'CFType')" UserInfo={NSDebugDescription=Property list invalid for format: 200 (property lists cannot contain objects of type 'CFType')}'
*** First throw call stack:
(
0   CoreFoundation                      0x000000010c196e65 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x000000010bc0fdeb objc_exception_throw + 48
2   UIKit                               0x000000010cbd5ba5 -[UIConcreteLocalNotification userInfo] + 0
3   App                            0x00000001088cb65b -[monitorLocationViewController createNotificationWithStore:andArounders:] + 683
4   App                            0x00000001088cb1ed -[monitorLocationViewController sendNotificationIfNeededForStore:] + 397
5   App                            0x00000001088cb021 -[monitorLocationViewController locationManager:didEnterRegion:] + 145
6   App                            0x00000001088cae9c __68-[monitorLocationViewController locationManager:didUpdateLocations:]_block_invoke + 284
7   CoreFoundation                      0x000000010c0f59ff __51-[__NSSetM enumerateObjectsWithOptions:usingBlock:]_block_invoke + 79
8   CoreFoundation                      0x000000010c0f590a -[__NSSetM enumerateObjectsWithOptions:usingBlock:] + 202
9   App                            0x00000001088cad02 -[monitorLocationViewController locationManager:didUpdateLocations:] + 402
10  CoreLocation                        0x000000010a8c5177 CLClientGetCapabilities + 20882
11  CoreLocation                        0x000000010a8c15d3 CLClientGetCapabilities + 5614
12  CoreLocation                        0x000000010a8bc0cd CLClientInvalidate + 923
13  CoreFoundation                      0x000000010c0c2a1c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
14  CoreFoundation                      0x000000010c0b86a5 __CFRunLoopDoBlocks + 341
15  CoreFoundation                      0x000000010c0b7e02 __CFRunLoopRun + 850
16  CoreFoundation                      0x000000010c0b7828 CFRunLoopRunSpecific + 488
17  GraphicsServices                    0x000000010ed92ad2 GSEventRunModal + 161
18  UIKit                               0x000000010c543610 UIApplicationMain + 171
19  App                            0x00000001088dedef main + 111
20  libdyld.dylib                       0x000000010dbbe92d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

有人知道为什么会崩溃吗?

谢谢!

【问题讨论】:

  • 不知何故你没有得到UILocalNotification 的子类。你的init 方法是什么样的?
  • 顺便说一句,以其他方式存储附加属性可能更有意义。 UILocalNotification 对象被传递给 iOS,而您的自定义类可能无法在转换过程中存活。
  • @Avi 我不会在customLocalNotification 上覆盖init,所以我猜这是默认设置。
  • @Avi 如何在没有自定义类的情况下将商店属性存储到 UILocalNotification 对象?
  • 看来你不能真正继承UILocalNotification,它的类永远是UIConcreteLocationNotification。我从UILocalNotification 继承然后从UIView 对其进行了测试,并且它的类不会因从UILocalNotification 继承而改变。您可能还对stackoverflow.com/questions/8580782/… 感兴趣,您可能还希望将自定义数据放入userInfo (NSDictionary) 以用于您未知的目的...

标签: ios objective-c cocoa-touch unrecognized-selector


【解决方案1】:

UILocalNotifications 旨在包含一些轻量级数据。也就是说,userInfo 只能包含 property list encodable 项。而且我要补充一点,整个事情并不是为了扔重物而设计的,而只是传递一些上下文信息,当通知到达时您需要处理这些信息(即一些标识符、数字等)。

【讨论】:

  • 如何通过UILocalNotification 传递像Store 对象(具有一些属性的自定义类)这样的对象? (我需要知道通知发送到哪个商店)。
  • 将你的商店编码成一个 NSDictionary,并在通知到达时解包。当然,整个过程只有在 Store 对象没有以无法轻松恢复的方式连接到应用程序的任何其他部分时才有意义。
  • Store 对象转换为NSDictionary 对象转换为userInfo 字典对象?
  • 是的。通常,您可能只存储例如 Store 的标识符,然后重建它。同样,这不适用于复杂的对象图。请记住,您的新实例与原始对象没有任何联系,并且它通常存在于全新的环境中(即应用启动后)。
  • 哦,那我如何使用通知获取原始对象?
【解决方案2】:

我同意 Eiko。 您可以将任意键值对添加到此字典中。但是,键和值必须是有效的属性列表类型;如果没有,则会引发异常。 检查以下两个链接 UILocalNotification Valid Property list types

要解决您的问题,您可以试试这个 1) 为您的对象存储创建 NSDictionary,将所有属性作为键值对,您希望通过 UILocalNotification 传递 2) 设置字典对象 userinfo 字典
3) 解析并加载你的新字典并在你想使用它的地方创建 Store 对象。

【讨论】:

  • 有什么选项可以让原始对象通过吗?
  • 您可能在 Store 类中有一些独特的键属性。只需将该键作为字符串传递给 UILocalNotification 并使用该键获取您的 Store 对象。
  • 您不能使用 userInfo 字典传递原始对象。所以你必须为此找到一些替代方法
猜你喜欢
  • 2017-02-19
  • 2019-08-28
  • 2012-07-24
相关资源
最近更新 更多