我正在编写一个Swift应用程序,使用Parse,我得到一个奇怪的" unrecognized selector sent to instance
"错误。首先,它只发生在我在iPhone 5(iPhone 5模拟器或iPhone 5c,我用于开发)上的应用程序上。其次,抛出错误的选择器实际上是属性,而不是方法。我尝试使用Instruments运行应用程序来搜索已解除分配的对象,但无法找到任何问题 - 对象似乎是正确的类型(自定义类Image)。
错误在以下方法中引发,罪魁祸首是isTextOnPicture
属性。
func prepareCell(image: Image) {
translatedText.frame = CGRectMake(translatedText.frame.origin.x,
image.isTextOnPicture ? originalText.frame.origin.y + originalText.frame.height : originalText.frame.origin.y,
translatedText.frame.width,
translatedText.frame.height)
originalText.hidden = image.isTextOnPicture
translatedText.hidden = true
bottomBarView.hidden = true
saveButton.hidden = true
translateButton.hidden = true
shareButton.hidden = true
translationSearchBar.hidden = true
translationSearchBar.text = ""
}
我的cellForItemAtIndexPath
中的UICollectionViewController
方法会调用此方法。这是Image-class:
class Image: PFObject, PFSubclassing {
@NSManaged var isTextOnPicture: Bool
@NSManaged var translations: [String: String]!
@NSManaged var tags: [String]!
@NSManaged var name: String!
@NSManaged var image: PFFile!
@NSManaged var textLanguage: Language!
override class func initialize() {
var onceToken : dispatch_once_t = 0;
dispatch_once(&onceToken) {
self.registerSubclass()
}
}
class func parseClassName() -> String {
return "Image"
}
}
编辑:
这是实际的错误消息:
2015-04-20 15:55:02.673 LesDudesApp[2592:52255] -[LesDudesApp.Image isTextOnPicture]: unrecognized selector sent to instance 0x7b60bda0
2015-04-20 15:55:02.690 LesDudesApp[2592:52255] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[LesDudesApp.Image isTextOnPicture]: unrecognized selector sent to instance 0x7b60bda0'
*** First throw call stack:
(
0 CoreFoundation 0x0106c746 __exceptionPreprocess + 182
1 libobjc.A.dylib 0x02fefa97 objc_exception_throw + 44
2 CoreFoundation 0x01074705 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
3 CoreFoundation 0x00fbb287 ___forwarding___ + 1047
4 CoreFoundation 0x00fbae4e _CF_forwarding_prep_0 + 14
5 LesDudesApp 0x000db4d6 _TFC11LesDudesApp16FunnyPictureCell11prepareCellfS0_FCS_5ImageT_ + 486
6 LesDudesApp 0x000bbd71 _TFC11LesDudesApp36FunnyPictureCollectionViewController14collectionViewfS0_FTCSo16UICollectionView22cellForItemAtIndexPathCSo11NSIndexPath_CSo20UICollectionViewCell + 737
7 LesDudesApp 0x000bd929 _TToFC11LesDudesApp36FunnyPictureCollectionViewController14collectionViewfS0_FTCSo16UICollectionView22cellForItemAtIndexPathCSo11NSIndexPath_CSo20UICollectionViewCell + 89
8 UIKit 0x0251fd5a -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 258
9 UIKit 0x02521e96 -[UICollectionView _updateVisibleCellsNow:] + 4947
10 UIKit 0x025266e1 -[UICollectionView layoutSubviews] + 281
11 UIKit 0x01ea557a -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 668
12 libobjc.A.dylib 0x03005771 -[NSObject performSelector:withObject:] + 70
13 QuartzCore 0x01c35e47 -[CALayer layoutSublayers] + 144
14 QuartzCore 0x01c29925 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 403
15 QuartzCore 0x01c2977a _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
16 QuartzCore 0x01b85c52 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 284
17 QuartzCore 0x01b870e5 _ZN2CA11Transaction6commitEv + 487
18 QuartzCore 0x01b877fc _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
19 CoreFoundation 0x00f8d86e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
20 CoreFoundation 0x00f8d7b0 __CFRunLoopDoObservers + 400
21 CoreFoundation 0x00f831ea __CFRunLoopRun + 1226
22 CoreFoundation 0x00f82a5b CFRunLoopRunSpecific + 443
23 CoreFoundation 0x00f8288b CFRunLoopRunInMode + 123
24 GraphicsServices 0x082632c9 GSEventRunModal + 192
25 GraphicsServices 0x08263106 GSEventRun + 104
26 UIKit 0x01e12106 UIApplicationMain + 1526
27 LesDudesApp 0x000b13b4 main + 180
28 libdyld.dylib 0x07259ac9 start + 1
29 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
堆栈跟踪:
cellForRowAtIndexPath
方法:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! FunnyPictureCell
let currentImage = self.images[indexPath.row] as Image
cell.image = currentImage
cell.delegate = self
cell.prepareCell(currentImage)
if indexPath.row == 0 {
User.currentUser()!.statistics.totalPicsViewed++
AppDelegate.getInstance().picsForSession++
}
currentImage.image.getDataInBackgroundWithBlock { (data: NSData?, error: NSError?) -> Void in
if error == nil && data != nil {
cell.funnyPictureImageView.image = UIImage(data: data!)
} else {
println ("error while loading image: \(error)")
}
}
cell.originalText.text = currentImage.translations[User.currentUser()!.motherTongue.language.lowercaseString]
cell.translatedText.text = currentImage.translations[User.currentUser()!.learningLanguage.language.lowercaseString]
return cell
}
答案 0 :(得分:0)
你确定isTextOnPicture在落入该函数时有一个值吗?
Optionals在有值时不再隐式评估为true,否则不再隐式评估为false,以避免在使用可选的Bool值时出现混淆。相反,使用==或!=运算符对nil进行显式检查,以确定可选项是否包含值。