我正在获取valueForUndefinedKey:]:此类不是键值错误的键值编码兼容

时间:2014-09-11 15:50:43

标签: ios cocoa-touch uitableview swift nsunknownkeyexception

我循环遍历一系列对象,然后获得一个关键值" resort"如果它还不存在,则将其存储在另一个数组中。

我的NSLog结果显示字符串在那里。但是我得到了这个问题标题中提到的错误。当我更换字符串" resortName"与"位置"并存储整个对象而不是错误消失。

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

对象是解析PFObjects的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) 

1 个答案:

答案 0 :(得分:0)

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

所以我宣布了实例var:

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
}