[英]“unrecognized selector sent to instance” Swift
I get no errors or anything, but when I run the app it crashes. 我没有收到任何错误或任何提示,但是当我运行应用程序时,它崩溃了。 The log I get is "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MPConcreteMediaItem imageWithSize:]: unrecognized selector sent to instance 0x14eefa3b0'".
我得到的日志是“由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[MPConcreteMediaItem imageWithSize:]:无法识别的选择器已发送到实例0x14eefa3b0'”。
I think the line that causes this problem is this one: 我认为导致此问题的那条线是这样的:
cell?.imageView?.image = sortedResults[indexPath.row].imageWithSize(imageSize)
because when I delete it/uncomment it, everything works fine. 因为当我删除/取消注释时,一切正常。
imageSize
is a CGSize var. imageSize
是CGSize变量。
The whole Code: 整个代码:
let startTime: NSTimeInterval = NSDate().timeIntervalSince1970
let songsQuery: MPMediaQuery = MPMediaQuery.songsQuery()
let songsArray: [MPMediaItem] = songsQuery.items!
let songsNSArray : NSArray = NSArray(array: songsArray)
let descriptor: NSSortDescriptor = NSSortDescriptor(key: MPMediaItemPropertyLastPlayedDate, ascending: false)
let sortedResults: NSArray = songsNSArray.sortedArrayUsingDescriptors([descriptor])
let finishTime: NSTimeInterval = NSDate().timeIntervalSince1970
NSLog("Execution took %f seconds to return %i results.", finishTime - startTime, sortedResults.count)
cell?.textLabel?.text = sortedResults[indexPath.row].title
cell?.detailTextLabel?.text = sortedResults[indexPath.row].artist
cell?.imageView?.image = sortedResults[indexPath.row].imageWithSize(imageSize)
I'm guessing object located in sortedResults[indexPath.row] is not what you think it is, and hence it has not got the method imageWithSize()
. 我猜测位于sortedResults [indexPath.row]中的对象不是您认为的那样,因此它没有方法
imageWithSize()
。 Object may be nil? 对象可能为零?
EDIT 编辑
To be safe, you could do something like this before using the object in the array: 为了安全起见,可以在使用数组中的对象之前执行以下操作:
if let validObject = sortedResults[indexPath.row] as MPConcreteMediaItem? {
// Can now use validObject safely, since we have checked that it is in fact an instance of MPConcreteMediaItem
}
You have added an object to your sortedResults
NSArray
that isn't the type you are expecting. 您已将一个对象添加到
sortedResults
NSArray
,该对象不是您期望的类型。 So when you call imageWithSize(imageSize: CGSize)
on that object the app crashes as the object doesn't respond to that selector (implement that method). 因此,当您在该对象上调用
imageWithSize(imageSize: CGSize)
时,应用会崩溃,因为该对象未响应该选择器(实现该方法)。
MPMediaItem
does not implement this method. MPMediaItem
不实现此方法。 Seems that you want to access the constant values on MPMediaItem? 似乎要访问MPMediaItem上的常量值?
General Media Item Property 通用媒体项目属性
Something like this may help: 这样的事情可能会有所帮助:
if let mediaItem: MPMediaItem = sortedResults[indexPath.row] as? MPMediaItem {
cell?.textLabel?.text = mediaItem.valueForProperty(MPMediaItemPropertyAlbumTitle) as? String
cell?.detailTextLabel?.text = mediaItem.valueForProperty(MPMediaItemPropertyAlbumArtist) as? String
if let artwork: MPMediaItemArtwork = mediaItem.valueForProperty(MPMediaItemPropertyArtwork) as? MPMediaItemArtwork {
cell?.imageView?.image = artwork.imageWithSize(imageSize)
}
}
Long term, I'd recommend implementing one of the sorting methods on a Swift array rather than hard converting to an NSArray to sort using a descriptor - this bug would have been caught at compile time instead. 从长远来看,我建议在Swift数组上实现一种排序方法,而不是使用描述符将其硬转换为NSArray进行排序-该错误将在编译时捕获。 Haven't checked, but something like this to keep things in Swift:
尚未检查,但是可以将以下内容保留在Swift中:
songsArray.sort { (itemOne, itemTwo) -> Bool in
return itemOne.lastPlayedDate?.laterDate(itemTwo.lastPlayedDate!) == itemOne.lastPlayedDate
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.