0

我正在遍历一个对象数组,然后获取键“resort”的值并将其存储在另一个数组中(如果它尚不存在)。

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

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

objects 是解析 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) 
4

1 回答 1

0

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

所以我声明了实例变量:

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-11T16:47:37.390 回答