简体   繁体   English

我怎么知道Xcode Objective-C在哪里引发错误?

[英]How I can know where raises the error in Xcode Objective-C?

I'm beginner in Objective-C, I'm struggling with this code that running in iOS 7.1 but crash in iOS 8. 我是Objective-C的初学者,我为在iOS 7.1中运行但在iOS 8中崩溃的这段代码感到困惑。

How I can know where is problem from stack trace? 如何从堆栈跟踪中知道问题出在哪里?

And why a code works in older version (iOS 7) and not work in newer version? 为什么代码在旧版本(iOS 7)中有效而在新版本中无效?

Is that a problem with using incorrect of NSMutableDictionary or NSArray ? 使用不正确的NSMutableDictionaryNSArray是否存在问题?

- (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; 
}

here is stack trace: 这是堆栈跟踪:

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) 

part of this question is duplicate of This question but as noob iOS developer I didn't even know where to start ask my question, so sorry for duplicate question, but in other questions, I get my own answer and checked it as right answer. 这个问题的一部分是重复的这个问题,但小白的iOS开发者我甚至不知道从哪里开始问我的问题,重复的问题,很抱歉,但在其他的问题,我得到我自己的答案,并检查它作为正确答案。

Assuming you're using Xcode you can turn on exception breakpoints. 假设您正在使用Xcode,则可以打开异常断点。 This will cause the application to jump to the line that has broken when an exception has been thrown. 当引发异常时,这将导致应用程序跳至中断的行。

在此处输入图片说明

The problem is you are trying to get the objectForKey for something that is not a dictionary but an NSData . 问题是您正试图为不是字典而是NSDataobjectForKey获取objectForKey I this it is this line: 我这是这条线:

UIImage* artworkImage = [UIImage imageWithData:artworkMetadataItem.value[@"data"]];

Maybe you can add s breakpoint there and print out artworkMetadataItem.value to see which type it is. 也许您可以在此处添加s断点并打印出artworkMetadataItem.value以查看其类型。

If this is not the problem you can try running through your code step by step to see what statement causes the crash. 如果这不是问题,则可以尝试逐步运行代码以查看导致崩溃的语句。

Hope this works though! 希望这行得通! Let me know how it goes 让我知道事情的后续

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎么知道正则表达式的哪一部分与Objective-c相匹配 - How can I know which part of regex is matched of Objective-c 使用带有Xcode 4.2的Objective-C ARC时,如何防止对象被释放? - How can I keep objects from being released when using Objective-C ARC with Xcode 4.2? 如何在Objective-C和Swift的Xcode 6中为临时日志记录和FIXME标记创建书签? - How can I create bookmarks for temporary logging and FIXME markers in Xcode 6 for both Objective-C and Swift? 如何使用XCode 8将URL中的JSON数据导入到Objective-C应用程序中? - How can I get the JSON data from a URL into my Objective-C application, Using XCode 8? 如何在混合Xcode项目(Objective-C和Swift)中使用XCGLogger - How can I use XCGLogger in a mixed Xcode project (Objective-C and Swift) 如何在Xcode中找到Objective-C函数的用法? - How do I find usages of an Objective-C function in Xcode? Xcode只能重构C和Objective-C代码。如何在Xcode 6中重命名swift类名? - Xcode can only refactor C and Objective-C code. How to rename swift class name in Xcode 6? 不确定将代码放在程序中的什么位置。 如何在 XCode 中通过触摸生成圆圈。 (目标-C) - Unsure where to put code in program. An how to have circles generated on touch in XCode. (Objective-C) 是否可以像执行C程序那样在Xcode中运行Objective-C程序(不是iPhone / Mac项目)? - Can I run Objective-C programs (not iPhone/Mac projects) in Xcode like executing C programs? Objective-C-如何将字节数组转换为NSString? - Objective-C - How can I convert Byte Array to NSString?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM