Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios I';m getting valueForUndefinedKey::此类不符合密钥错误的密钥值编码_Ios_Cocoa Touch_Uitableview_Swift_Nsunknownkeyexception - Fatal编程技术网

Ios I';m getting valueForUndefinedKey::此类不符合密钥错误的密钥值编码

Ios I';m getting valueForUndefinedKey::此类不符合密钥错误的密钥值编码,ios,cocoa-touch,uitableview,swift,nsunknownkeyexception,Ios,Cocoa Touch,Uitableview,Swift,Nsunknownkeyexception,我在一个对象数组中循环,然后获取key“resort”的值,如果它不存在,则将其存储在另一个数组中 我的NSLog结果显示字符串在那里。然而,我得到了这个问题的标题中提到的错误。当我将字符串“restorname”替换为“location”并存储整个对象时,错误就消失了 问题是我需要检查字符串,因为这是在这种情况下阻止存储重复对象的唯一方法 对象是对象的NSArray 这是我的代码: override func numberOfSectionsInTableView(tableView: UIT

我在一个对象数组中循环,然后获取key“resort”的值,如果它不存在,则将其存储在另一个数组中

我的NSLog结果显示字符串在那里。然而,我得到了这个问题的标题中提到的错误。当我将字符串“restorname”替换为“location”并存储整个对象时,错误就消失了

问题是我需要检查字符串,因为这是在这种情况下阻止存储重复对象的唯一方法

对象是对象的NSArray

这是我的代码:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    for location in objects {

        var resortName = location.valueForKey("resort") as NSString
        NSLog("resortname: \(resortName)")

        if !_sections.containsObject(resortName) {
            _sections.addObject(resortName)
        }
    }

    NSLog("sections: \(_sections.count)")
    return _sections.count
}
2014-09-11 16:42:08.635 CharityApp[33639:380242] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x7fa325842340> valueForUndefinedKey:]: this class is not key value coding-compliant for the key resort.'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010f8293f5 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001113febb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010f829039 -[NSException raise] + 9
    3   Foundation                          0x000000010fcd0fba -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 226
    4   Foundation                          0x000000010fc29543 -[NSObject(NSKeyValueCoding) valueForKey:] + 251
    5   CharityApp                          0x000000010df14ed0 _TFC8CharityApp31ActivityListTableViewController9tableViewfS0_FTCSo11UITableView23titleForHeaderInSectionSi_GSqSS_ + 976
    6   CharityApp                          0x000000010df150ac _TToFC8CharityApp31ActivityListTableViewController9tableViewfS0_FTCSo11UITableView23titleForHeaderInSectionSi_GSqSS_ + 92
    7   UIKit                               0x000000011024c8e1 -[UITableView _delegateWantsHeaderTitleForSection:] + 56
    8   UIKit                               0x000000011024c8a5 -[UITableView _delegateWantsHeaderForSection:] + 572
    9   UIKit                               0x00000001103cf81e -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 162
    10  UIKit                               0x00000001103d55ec -[UITableViewRowData rectForSection:] + 302
    11  UIKit                               0x00000001103d6583 -[UITableViewRowData indexPathsForRowsInRect:] + 90
    12  UIKit                               0x000000011023f0ed -[UITableView indexPathsForVisibleRows] + 269
    13  CharityApp                          0x000000010df5f39a -[PFQueryTableViewController loadImagesForOnscreenRows] + 137
    14  UIKit                               0x00000001101efb4f -[UIScrollView(UIScrollViewInternal) _stopScrollDecelerationNotify:] + 277
    15  UIKit                               0x00000001101efd69 -[UIScrollView(UIScrollViewInternal) _stopScrollingNotify:pin:tramplingDragFlags:] + 479
    16  UIKit                               0x00000001101ea53a -[UIScrollView _smoothScrollWithUpdateTime:] + 2896
    17  QuartzCore                          0x000000010e54a8f7 _ZN2CA7Display15DisplayLinkItem8dispatchEv + 37
    18  QuartzCore                          0x000000010e54a7bf _ZN2CA7Display11DisplayLink14dispatch_itemsEyyy + 315
    19  CoreFoundation                      0x000000010f7914e4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
    20  CoreFoundation                      0x000000010f7910a5 __CFRunLoopDoTimer + 1045
    21  CoreFoundation                      0x000000010f7543dd __CFRunLoopRun + 1901
    22  CoreFoundation                      0x000000010f753a06 CFRunLoopRunSpecific + 470
    23  GraphicsServices                    0x0000000111bc19f0 GSEventRunModal + 161
    24  UIKit                               0x000000011015a550 UIApplicationMain + 1282
    25  CharityApp                          0x000000010dedd7ae top_level_code + 78
    26  CharityApp                          0x000000010dedd8ca main + 42
    27  libdyld.dylib                       0x00000001122ca145 start + 1
    28  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 
var _sections = [String]() 
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    for location in objects {

        var resortName = location["resort"] as NSString
        NSLog("resortname: \(resortName)")

        if !contains(_sections, resortName) {
            _sections.append(resortName)
        }

    }

    NSLog("sections: \(_sections.count)")
    return _sections.count
}
知道我做错了什么吗

谢谢你抽出时间

完整堆栈:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    for location in objects {

        var resortName = location.valueForKey("resort") as NSString
        NSLog("resortname: \(resortName)")

        if !_sections.containsObject(resortName) {
            _sections.addObject(resortName)
        }
    }

    NSLog("sections: \(_sections.count)")
    return _sections.count
}
2014-09-11 16:42:08.635 CharityApp[33639:380242] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x7fa325842340> valueForUndefinedKey:]: this class is not key value coding-compliant for the key resort.'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010f8293f5 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001113febb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010f829039 -[NSException raise] + 9
    3   Foundation                          0x000000010fcd0fba -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 226
    4   Foundation                          0x000000010fc29543 -[NSObject(NSKeyValueCoding) valueForKey:] + 251
    5   CharityApp                          0x000000010df14ed0 _TFC8CharityApp31ActivityListTableViewController9tableViewfS0_FTCSo11UITableView23titleForHeaderInSectionSi_GSqSS_ + 976
    6   CharityApp                          0x000000010df150ac _TToFC8CharityApp31ActivityListTableViewController9tableViewfS0_FTCSo11UITableView23titleForHeaderInSectionSi_GSqSS_ + 92
    7   UIKit                               0x000000011024c8e1 -[UITableView _delegateWantsHeaderTitleForSection:] + 56
    8   UIKit                               0x000000011024c8a5 -[UITableView _delegateWantsHeaderForSection:] + 572
    9   UIKit                               0x00000001103cf81e -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 162
    10  UIKit                               0x00000001103d55ec -[UITableViewRowData rectForSection:] + 302
    11  UIKit                               0x00000001103d6583 -[UITableViewRowData indexPathsForRowsInRect:] + 90
    12  UIKit                               0x000000011023f0ed -[UITableView indexPathsForVisibleRows] + 269
    13  CharityApp                          0x000000010df5f39a -[PFQueryTableViewController loadImagesForOnscreenRows] + 137
    14  UIKit                               0x00000001101efb4f -[UIScrollView(UIScrollViewInternal) _stopScrollDecelerationNotify:] + 277
    15  UIKit                               0x00000001101efd69 -[UIScrollView(UIScrollViewInternal) _stopScrollingNotify:pin:tramplingDragFlags:] + 479
    16  UIKit                               0x00000001101ea53a -[UIScrollView _smoothScrollWithUpdateTime:] + 2896
    17  QuartzCore                          0x000000010e54a8f7 _ZN2CA7Display15DisplayLinkItem8dispatchEv + 37
    18  QuartzCore                          0x000000010e54a7bf _ZN2CA7Display11DisplayLink14dispatch_itemsEyyy + 315
    19  CoreFoundation                      0x000000010f7914e4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
    20  CoreFoundation                      0x000000010f7910a5 __CFRunLoopDoTimer + 1045
    21  CoreFoundation                      0x000000010f7543dd __CFRunLoopRun + 1901
    22  CoreFoundation                      0x000000010f753a06 CFRunLoopRunSpecific + 470
    23  GraphicsServices                    0x0000000111bc19f0 GSEventRunModal + 161
    24  UIKit                               0x000000011015a550 UIApplicationMain + 1282
    25  CharityApp                          0x000000010dedd7ae top_level_code + 78
    26  CharityApp                          0x000000010dedd8ca main + 42
    27  libdyld.dylib                       0x00000001122ca145 start + 1
    28  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 
var _sections = [String]() 
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    for location in objects {

        var resortName = location["resort"] as NSString
        NSLog("resortname: \(resortName)")

        if !contains(_sections, resortName) {
            _sections.append(resortName)
        }

    }

    NSLog("sections: \(_sections.count)")
    return _sections.count
}
2014-09-11 16:42:08.635慈善应用程序[33639:380242]***由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[valueForUndefinedKey:]:此类不符合关键度假村的键值编码。”
***第一次抛出调用堆栈:
(
0 CoreFoundation 0x000000010f8293f5例外预处理+165
1 libobjc.A.dylib 0x000000011113FEBB7 objc_异常_抛出+45
2 CoreFoundation 0x000000010f829039-[N异常升高]+9
3基金会0x000 000 010FCD0FBA- [ NSO对象(NKEY ValueCudid)ValueFunDealEdKEY:] + 226
4基金会0x000 000 010FC9533-[NSbOuts](NSKEY ValueCudid)ValueFoiKe::+ 251
5 CharityApp 0x000000010df14ed0\u TFC8 CharityApp31活动列表表视图控制器9表视图FS0\u FTCSO11实用视图23标题为GSQS+976的标题部分
6 CharityApp 0x000000010df150ac\u TTOFC8 CharityApp31活动列表表视图控制器9表视图FS0\u FTCSO11实用视图23标题为GSQS+92的标题部分
7 UIKit 0x000000011024c8e1-[UITableView\u delegateWantHeaderTitleForSection:+56
8 UIKit 0x000000011024c8a5-[UITableView_delegateWantsHeaderPerfection:+572
9 UIKit 0x00000001103cf81e-[UISectionRowData refreshWithSection:tableView:tableViewRowData:+162
10 UIKit 0x00000001103d55ec-[UITableViewRowData rectForSection:][302
11 UIKit 0x00000001103d6583-[UITableViewRowData IndExpathsErrorsInRect:+90
12 UIKit 0x000000011023f0ed-[UITableView indexPathsForVisibleRows]+269
13 CharityApp 0x000000010df5f39a-[PFQueryTableViewController加载屏幕行图像]+137
14 UIKit 0x00000001101efb4f-[UIScrollView(UIScrollViewInternal)\u停止滚动减速通知:+277
15 UIKit 0x00000001101efd69-[UIScrollView(UIScrollViewInternal)\停止滚动通知:pin:tramplingDragFlags:+479
16 UIKit 0x00000001101ea53a-[UIScrollView\u smoothScrollWithUpdateTime:][2896
17夸脱核心0x000000010e54a8f7_ZN2CA7Display15DisplayLinkItem8dispatchEv+37
18夸脱核心0x000000010e54a7bf\u ZN2CA7Display11显示链接14调度项目Y+315
19 CoreFoundation 0x000000010f7914e4\uuu CFRUNLOOP\u正在调用\u OUT\u到\u计时器\u回调函数\uuuu+20
20 CoreFoundation 0x000000010f7910a5\uuU CFRunLoopDoTimer+1045
21 CoreFoundation 0x000000010f7543dd\uuu CFRunLoopRun+1901
22 CoreFoundation 0x000000010f753a06 CFRUNLOOPRUN特定+470
23图形服务0x0000000111bc19f0 GSEventRunModal+161
24 UIKit 0x000000011015a550 UIApplicationMain+1282
25慈善应用0x000000010dedd7ae顶级代码+78
26慈善应用0x000000010dedd8ca main+42
27 libdyld.dylib 0x00000001122ca145开始+1
28°?0x0000000000000001 0x0+1
)
libc++abi.dylib:以NSException类型的未捕获异常终止
(lldb)

我的解决方案是使用Swift数组而不是NSMUTABLEARRY

所以我声明了实例变量:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    for location in objects {

        var resortName = location.valueForKey("resort") as NSString
        NSLog("resortname: \(resortName)")

        if !_sections.containsObject(resortName) {
            _sections.addObject(resortName)
        }
    }

    NSLog("sections: \(_sections.count)")
    return _sections.count
}
2014-09-11 16:42:08.635 CharityApp[33639:380242] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x7fa325842340> valueForUndefinedKey:]: this class is not key value coding-compliant for the key resort.'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010f8293f5 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001113febb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010f829039 -[NSException raise] + 9
    3   Foundation                          0x000000010fcd0fba -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 226
    4   Foundation                          0x000000010fc29543 -[NSObject(NSKeyValueCoding) valueForKey:] + 251
    5   CharityApp                          0x000000010df14ed0 _TFC8CharityApp31ActivityListTableViewController9tableViewfS0_FTCSo11UITableView23titleForHeaderInSectionSi_GSqSS_ + 976
    6   CharityApp                          0x000000010df150ac _TToFC8CharityApp31ActivityListTableViewController9tableViewfS0_FTCSo11UITableView23titleForHeaderInSectionSi_GSqSS_ + 92
    7   UIKit                               0x000000011024c8e1 -[UITableView _delegateWantsHeaderTitleForSection:] + 56
    8   UIKit                               0x000000011024c8a5 -[UITableView _delegateWantsHeaderForSection:] + 572
    9   UIKit                               0x00000001103cf81e -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 162
    10  UIKit                               0x00000001103d55ec -[UITableViewRowData rectForSection:] + 302
    11  UIKit                               0x00000001103d6583 -[UITableViewRowData indexPathsForRowsInRect:] + 90
    12  UIKit                               0x000000011023f0ed -[UITableView indexPathsForVisibleRows] + 269
    13  CharityApp                          0x000000010df5f39a -[PFQueryTableViewController loadImagesForOnscreenRows] + 137
    14  UIKit                               0x00000001101efb4f -[UIScrollView(UIScrollViewInternal) _stopScrollDecelerationNotify:] + 277
    15  UIKit                               0x00000001101efd69 -[UIScrollView(UIScrollViewInternal) _stopScrollingNotify:pin:tramplingDragFlags:] + 479
    16  UIKit                               0x00000001101ea53a -[UIScrollView _smoothScrollWithUpdateTime:] + 2896
    17  QuartzCore                          0x000000010e54a8f7 _ZN2CA7Display15DisplayLinkItem8dispatchEv + 37
    18  QuartzCore                          0x000000010e54a7bf _ZN2CA7Display11DisplayLink14dispatch_itemsEyyy + 315
    19  CoreFoundation                      0x000000010f7914e4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
    20  CoreFoundation                      0x000000010f7910a5 __CFRunLoopDoTimer + 1045
    21  CoreFoundation                      0x000000010f7543dd __CFRunLoopRun + 1901
    22  CoreFoundation                      0x000000010f753a06 CFRunLoopRunSpecific + 470
    23  GraphicsServices                    0x0000000111bc19f0 GSEventRunModal + 161
    24  UIKit                               0x000000011015a550 UIApplicationMain + 1282
    25  CharityApp                          0x000000010dedd7ae top_level_code + 78
    26  CharityApp                          0x000000010dedd8ca main + 42
    27  libdyld.dylib                       0x00000001122ca145 start + 1
    28  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 
var _sections = [String]() 
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    for location in objects {

        var resortName = location["resort"] as NSString
        NSLog("resortname: \(resortName)")

        if !contains(_sections, resortName) {
            _sections.append(resortName)
        }

    }

    NSLog("sections: \(_sections.count)")
    return _sections.count
}
然后编辑了我的方法:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    for location in objects {

        var resortName = location.valueForKey("resort") as NSString
        NSLog("resortname: \(resortName)")

        if !_sections.containsObject(resortName) {
            _sections.addObject(resortName)
        }
    }

    NSLog("sections: \(_sections.count)")
    return _sections.count
}
2014-09-11 16:42:08.635 CharityApp[33639:380242] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x7fa325842340> valueForUndefinedKey:]: this class is not key value coding-compliant for the key resort.'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010f8293f5 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001113febb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010f829039 -[NSException raise] + 9
    3   Foundation                          0x000000010fcd0fba -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 226
    4   Foundation                          0x000000010fc29543 -[NSObject(NSKeyValueCoding) valueForKey:] + 251
    5   CharityApp                          0x000000010df14ed0 _TFC8CharityApp31ActivityListTableViewController9tableViewfS0_FTCSo11UITableView23titleForHeaderInSectionSi_GSqSS_ + 976
    6   CharityApp                          0x000000010df150ac _TToFC8CharityApp31ActivityListTableViewController9tableViewfS0_FTCSo11UITableView23titleForHeaderInSectionSi_GSqSS_ + 92
    7   UIKit                               0x000000011024c8e1 -[UITableView _delegateWantsHeaderTitleForSection:] + 56
    8   UIKit                               0x000000011024c8a5 -[UITableView _delegateWantsHeaderForSection:] + 572
    9   UIKit                               0x00000001103cf81e -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 162
    10  UIKit                               0x00000001103d55ec -[UITableViewRowData rectForSection:] + 302
    11  UIKit                               0x00000001103d6583 -[UITableViewRowData indexPathsForRowsInRect:] + 90
    12  UIKit                               0x000000011023f0ed -[UITableView indexPathsForVisibleRows] + 269
    13  CharityApp                          0x000000010df5f39a -[PFQueryTableViewController loadImagesForOnscreenRows] + 137
    14  UIKit                               0x00000001101efb4f -[UIScrollView(UIScrollViewInternal) _stopScrollDecelerationNotify:] + 277
    15  UIKit                               0x00000001101efd69 -[UIScrollView(UIScrollViewInternal) _stopScrollingNotify:pin:tramplingDragFlags:] + 479
    16  UIKit                               0x00000001101ea53a -[UIScrollView _smoothScrollWithUpdateTime:] + 2896
    17  QuartzCore                          0x000000010e54a8f7 _ZN2CA7Display15DisplayLinkItem8dispatchEv + 37
    18  QuartzCore                          0x000000010e54a7bf _ZN2CA7Display11DisplayLink14dispatch_itemsEyyy + 315
    19  CoreFoundation                      0x000000010f7914e4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
    20  CoreFoundation                      0x000000010f7910a5 __CFRunLoopDoTimer + 1045
    21  CoreFoundation                      0x000000010f7543dd __CFRunLoopRun + 1901
    22  CoreFoundation                      0x000000010f753a06 CFRunLoopRunSpecific + 470
    23  GraphicsServices                    0x0000000111bc19f0 GSEventRunModal + 161
    24  UIKit                               0x000000011015a550 UIApplicationMain + 1282
    25  CharityApp                          0x000000010dedd7ae top_level_code + 78
    26  CharityApp                          0x000000010dedd8ca main + 42
    27  libdyld.dylib                       0x00000001122ca145 start + 1
    28  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 
var _sections = [String]() 
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    for location in objects {

        var resortName = location["resort"] as NSString
        NSLog("resortname: \(resortName)")

        if !contains(_sections, resortName) {
            _sections.append(resortName)
        }

    }

    NSLog("sections: \(_sections.count)")
    return _sections.count
}

在你的问题中张贴完整准确的错误。@rmaddy刚刚添加了它。如果它是Objective-C,我会说你的数组对象之一是字符串,而不是
位置
应该是什么。我不知道这种分析是否可以移植。@PhillipMills我最终放弃了,改用了Swift数组,幸运的是它成功了。