Ios 我如何知道Xcode Objective-C中哪里会产生错误?

Ios 我如何知道Xcode Objective-C中哪里会产生错误?,ios,objective-c,ios8,Ios,Objective C,Ios8,我是Objective-C的初学者,我正在努力处理这段在iOS 7.1中运行但在iOS 8中崩溃的代码 如何从堆栈跟踪中知道问题在哪里 为什么代码可以在旧版本(iOS 7)中工作而不能在新版本中工作? 这是使用不正确的NSMutableDictionary或NSArray的问题吗 - (NSDictionary*) MPNowPlayingInfoCenterNowPlayingInfo { NSMutableDictionary *info; if (self.current

我是Objective-C的初学者,我正在努力处理这段在iOS 7.1中运行但在iOS 8中崩溃的代码

如何从堆栈跟踪中知道问题在哪里

为什么代码可以在旧版本(iOS 7)中工作而不能在新版本中工作?

这是使用不正确的
NSMutableDictionary
NSArray
的问题吗

- (NSDictionary*) MPNowPlayingInfoCenterNowPlayingInfo {
    NSMutableDictionary *info;

    if (self.currentPlayable && self.currentPlayable.mediaItemProperties) {
        info = self.currentPlayable.mediaItemProperties.mutableCopy;
    } else {
        info = [NSMutableDictionary dictionaryWithCapacity:10];
    }

    //    Set defaults if missing
    NSArray* metadata = self.player.currentItem.asset.commonMetadata;

    if (!info[MPMediaItemPropertyPlaybackDuration]) {
        float _playbackDuration = self.currentPlayerItem ? CMTimeGetSeconds(self.currentPlayerItem.duration) : 0.0f;
        NSNumber* playbackDuration = @(_playbackDuration);
        info[MPMediaItemPropertyPlaybackDuration] = playbackDuration;
    }

    if (!info[MPNowPlayingInfoPropertyElapsedPlaybackTime]) {
        float _elapsedPlaybackTime = self.currentPlayerItem ? CMTimeGetSeconds(self.currentPlayerItem.currentTime) : 0.0f;
        NSNumber* elapsedPlaybackTime = @(_elapsedPlaybackTime);
        info[MPNowPlayingInfoPropertyElapsedPlaybackTime] = elapsedPlaybackTime;
    }

    if (!info[MPMediaItemPropertyArtwork]) {
        NSArray* artworkMetadata = [AVMetadataItem metadataItemsFromArray:metadata
                                                                  withKey:AVMetadataCommonKeyArtwork
                                                                 keySpace:AVMetadataKeySpaceCommon];
        if (artworkMetadata.count > 0) {
            AVMetadataItem* artworkMetadataItem = artworkMetadata[0];

            UIImage* artworkImage = [UIImage imageWithData:artworkMetadataItem.value[@"data"]];
            MPMediaItemArtwork* artwork = [[MPMediaItemArtwork alloc] initWithImage:artworkImage];

            info[MPMediaItemPropertyArtwork] = artwork;
        }
    }

    if (!info[MPMediaItemPropertyTitle]) {
        NSArray* _metadata = [AVMetadataItem metadataItemsFromArray:metadata withKey:AVMetadataCommonKeyTitle keySpace:AVMetadataKeySpaceCommon];

        if (_metadata.count > 0) {
            AVMetadataItem* _metadataItem = _metadata[0];
            info[MPMediaItemPropertyTitle] = _metadataItem.value;
        }
    }

    if (!info[MPMediaItemPropertyAlbumTitle]) {
        NSArray* _metadata = [AVMetadataItem metadataItemsFromArray:metadata withKey:AVMetadataCommonKeyAlbumName keySpace:AVMetadataKeySpaceCommon];

        if (_metadata.count > 0) {
            AVMetadataItem* _metadataItem = _metadata[0];
            info[MPMediaItemPropertyAlbumTitle] = _metadataItem.value;
        }
    }

    if (!info[MPMediaItemPropertyArtist]) {
        NSArray* _metadata = [AVMetadataItem metadataItemsFromArray:metadata withKey:AVMetadataCommonKeyArtist keySpace:AVMetadataKeySpaceCommon];

        if (_metadata.count > 0) {
            AVMetadataItem* _metadataItem = _metadata[0];
            info[MPMediaItemPropertyArtist] = _metadataItem.value;
        }
    }

    return info; 
}
下面是堆栈跟踪:

HelloCordova[20791:74393] THREAD WARNING: ['AudioPlayerPlugin'] took '248.958008' ms. Plugin should use a background thread.
2015-04-01 10:16:21.377 HelloCordova[20791:74903] Retrieving lock screen art...
2015-04-01 10:16:29.295 HelloCordova[20791:74903] Initializing lock screen art...
2015-04-01 10:16:29.296 HelloCordova[20791:74903] Creating MPMediaItemArtwork...
2015-04-01 10:16:29.322 HelloCordova[20791:74903] Done retrieving lock screen art.
2015-04-01 10:16:51.439 HelloCordova[20791:74393] -[__NSCFData objectForKeyedSubscript:]: unrecognized selector sent to instance 0x7ffb33e0fe80
Apr  1 10:16:51 Pourias-Mac.local rtcreporting[20791] <Info>: logging starts...
Apr  1 10:16:51 Pourias-Mac.local rtcreporting[20791] <Debug>: setMessageLoggingBlock: called
2015-04-01 10:16:51.618 HelloCordova[20791:74393] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFData objectForKeyedSubscript:]: unrecognized selector sent to instance 0x7ffb33e0fe80'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010241ea75 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000104f96bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x0000000102425d1d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010237d9dc ___forwarding___ + 988
    4   CoreFoundation                      0x000000010237d578 _CF_forwarding_prep_0 + 120
    5   HelloCordova                        0x00000001021ce2a8 -[PRXPlayer MPNowPlayingInfoCenterNowPlayingInfo] + 2056
    6   HelloCordova                        0x00000001021d4549 -[NYPRPlayer MPNowPlayingInfoCenterNowPlayingInfo] + 57
    7   HelloCordova                        0x00000001021ce9e5 -[PRXPlayer setMPNowPlayingInfoCenterNowPlayingInfo] + 85
    8   HelloCordova                        0x00000001021d5acb -[NYPRPlayer refreshMetadata] + 43
    9   HelloCordova                        0x00000001021c4e02 -[AudioStreamHandler updateProgress] + 146
    10  HelloCordova                        0x00000001021c576b -[AudioStreamHandler observedPlayerDidObservePeriodicTimeInterval:] + 91
    11  HelloCordova                        0x00000001021cc85e -[PRXPlayer reportPlayerTimeIntervalToObservers] + 670
    12  HelloCordova                        0x00000001021caa4b -[PRXPlayer playerItemDidJumpTime:] + 91
    13  CoreFoundation                      0x00000001023ee7fc __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
    14  CoreFoundation                      0x00000001022ee204 _CFXNotificationPost + 2484
    15  AVFoundation                        0x0000000103b4c0b2 __avplayeritem_fpItemNotificationCallback_block_invoke + 7447
    16  libdispatch.dylib                   0x00000001056b8186 _dispatch_call_block_and_release + 12
    17  libdispatch.dylib                   0x00000001056d7614 _dispatch_client_callout + 8
    18  libdispatch.dylib                   0x00000001056bfa1c _dispatch_main_queue_callback_4CF + 1664
    19  CoreFoundation                      0x0000000102386749 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    20  CoreFoundation                      0x000000010234962b __CFRunLoopRun + 2043
    21  CoreFoundation                      0x0000000102348bc6 CFRunLoopRunSpecific + 470
    22  GraphicsServices                    0x0000000106735a58 GSEventRunModal + 161
    23  UIKit                               0x000000010280e580 UIApplicationMain + 1282
    24  HelloCordova                        0x000000010219a0e7 main + 71
    25  libdyld.dylib                       0x000000010570c145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 
HelloCordova[20791:74393]线程警告:['AudioPlayerPlugin']采用了'248.958008'ms。插件应使用后台线程。
2015-04-01 10:16:21.377 HelloCordova[20791:74903]检索锁屏艺术。。。
2015-04-01 10:16:29.295 HelloCordova[20791:74903]初始化锁屏艺术。。。
2015-04-01 10:16:29.296 HelloCordova[20791:74903]创建MPMediaItemArtwork。。。
2015-04-01 10:16:29.322 HelloCordova[20791:74903]完成检索锁屏艺术。
2015-04-01 10:16:51.439 HelloCordova[20791:74393]-[\uu NSCFData objectforkeydsubscript:]:发送到实例0x7ffb33e0fe80的选择器无法识别
Apr 1 10:16:51 Pourias-Mac.local RTC报告[20791]:日志记录开始。。。
Apr 1 10:16:51 Pourias-Mac.local RTC报告[20791]:setMessageLoggingBlock:调用
2015-04-01 10:16:51.618 HelloCordova[20791:74393]***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[\uu NSCFData objectforkeydsubscript::]:未识别的选择器发送到实例0x7ffb33e0fe80'
***第一次抛出调用堆栈:
(
0 CoreFoundation 0x000000010241ea75例外预处理+165
1 libobjc.A.dylib 0x0000000104f96bb7 objc_异常_抛出+45
2 CoreFoundation 0x0000000102425d1d-[NSObject(NSObject)不识别选择器:][205
3 CoreFoundation 0x000000010237d9dc\uuuuuuuuuuuuuuuu+988
4 CoreFoundation 0x000000010237d578\u CF\u转发\u准备\u 0+120
5 HelloCordova 0x00000001021ce2a8-[PRXPlayer MPNowPlayingFocusEnterNowPlayingInfo]+2056
6 HelloCordova 0x00000001021d4549-[NYPRPlayer MPNowPlayingFocusEnterNowPlayingInfo]+57
7 HelloCordova 0x00000001021ce9e5-[PRXPlayer设置MPNowPlayingFocusEnterNowPlayingInfo]+85
8 HelloCordova 0x00000001021d5acb-[NYPRPlayer刷新元数据]+43
9 HelloCordova 0x00000001021c4e02-[AudioStreamHandler updateProgress]+146
10 HelloCordova 0x00000001021c576b-[AudioStreamHandler观察到的播放器IDobservePeriodictionInterval:+91
11 HelloCordova 0x00000001021cc85e-[PRXPlayer ReportPlayerTimeIntervalToObserver]+670
12 HelloCordova 0x00000001021CAA4-[PRXPlayer playerItemDidJumpTime:+91
13 CoreFoundation 0x00000001023ee7fc通知中心正在召集观察员
14 CoreFoundation 0x00000001022ee204_CFXNotificationPost+2484
15 AVFoundation 0x0000000103b4c0b2_uuAVPlayerItem_fpItemNotificationCallback_block_invoke+7447
16 libdispatch.dylib 0x00000001056b8186调度调用块和释放+12
17 libdispatch.dylib 0x00000001056d7614 _dispatch_client_callout+8
18 libdispatch.dylib 0x00000001056bfa1c_调度_主队列_回调_4CF+1664
19 CoreFoundation 0x0000000102386749\uuuu CFRUNLOOP\u正在为\u主\u调度\u队列\uuuu9提供服务
20 CoreFoundation 0x000000010234962b\uuu CFRunLoopRun+2043
21 CoreFoundation 0x0000000102348bc6 CFRunLoopRunSpecific+470
22图形服务0x0000000106735a58 GSEventRunModal+161
23 UIKit 0x000000010280e580 UIApplicationMain+1282
24 HelloCordova 0x000000010219a0e7干管+71
25 libdyld.dylib 0x000000010570c145启动+1
)
libc++abi.dylib:以NSException类型的未捕获异常终止
(lldb)


这个问题的一部分是重复的,但作为noob iOS开发人员,我甚至不知道从哪里开始问我的问题,所以很抱歉重复了这个问题,但在其他问题中,我得到了自己的答案并将其作为正确答案进行了检查。

假设您使用的是Xcode,您可以打开异常断点。这将导致应用程序跳转到引发异常时中断的行


问题在于,您试图获取的对象不是字典而是NSData的
objectForKey
。我是这样说的:

UIImage* artworkImage = [UIImage imageWithData:artworkMetadataItem.value[@"data"]];
也许您可以在那里添加断点并打印出
artworkMetadataItem.value
以查看它是哪种类型

如果这不是问题所在,您可以尝试一步一步地运行代码,看看是什么语句导致了崩溃


希望这能奏效!让我知道它是如何进行的

断点呢?可能重复的就是错误发生的地方:[PRXPlayer mpnowplayingfocusenternowplayinginfo]+2056可能重复的是,您提到的那行是有问题的,我四处搜索,发现他们改变了iOS 8中艺术品的数据类型,很高兴你找到了:)很高兴我能帮上忙