删除NSUserDefaults值时出错

时间:2015-07-29 08:15:00

标签: ios swift nsuserdefaults

我尝试使用此代码删除NSUserDefaults中的值:

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {
        self.DataFromOtherClass.removeObjectForKey("array")
        self.ShoppingListTable.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        self.ShoppingListTable.reloadData()
    }
}

变量DataFromAnotherClass包含NSUserDefaults.standardUserDefaults().objectForKey("array"),因为这与我在另一个类中用于另一个变量的代码相同。通过这样做,我想在这个类中调用该值,所以我可以删除它。 现在,当我滑动删除单元格时,我在调试器中收到此错误消息:

2015-07-08 12:37:59.502 Shoppy[12817:571298] -[__NSCFArray removeObjectForKey:]: unrecognized selector sent to instance 0x7be8bfc0
2015-07-08 12:37:59.580 Shoppy[12817:571298] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray removeObjectForKey:]: unrecognized selector sent to instance 0x7be8bfc0'
*** First throw call stack:
(
0   CoreFoundation                      0x0029a746 __exceptionPreprocess + 182
1   libobjc.A.dylib                     0x024a8a97 objc_exception_throw + 44
2   CoreFoundation                      0x002a2705 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
3   CoreFoundation                      0x001e9287 ___forwarding___ + 1047
4   CoreFoundation                      0x001e8e4e _CF_forwarding_prep_0 + 14
5   Shoppy                              0x00063415
_TFC6Shoppy19ThirdViewController9tableViewfS0_FTCSo11UITableView18commitEditingStyleOSC27UITableViewCellEditingStyle17forRowAtIndexPathCSo11NSIndexPath_T_ + 341
6   Shoppy                              0x000636e7 
_TToFC6Shoppy19ThirdViewController9tableViewfS0_FTCSo11UITableView18commitEditingStyleOSC27UITableViewCellEditingStyle17forRowAtIndexPathCSo11NSIndexPath_T_ + 103
7   UIKit                               0x00bed32a -[UITableView animateDeletionOfRowWithCell:] + 193
8   UIKit                               0x00bc60a6 __52-[UITableView 
_swipeActionButtonsForRowAtIndexPath:]_block_invoke + 102
9   UIKit                               0x00beea90 -[UITableView _actionButton:pushedInCell:] + 100
10  UIKit                               0x00dd192c -[UITableViewCell _actionButtonPushed:] + 65
11  libobjc.A.dylib                     0x024be7cd -[NSObject performSelector:withObject:withObject:] + 84
12  UIKit                               0x00abfa40 -[UIApplication sendAction:to:from:forEvent:] + 99
13  UIKit                               0x00abf9d2 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 64
14  UIKit                               0x00c0013a -[UIControl sendAction:to:forEvent:] + 69
15  UIKit                               0x00c00557 -[UIControl _sendActionsForEvents:withEvent:] + 598
16  UIKit                               0x00bff7c1 -[UIControl touchesEnded:withEvent:] + 660
17  UIKit                               0x00ef9a27 _UIGestureRecognizerUpdate + 13225
18  UIKit                               0x00b1790b -[UIWindow _sendGesturesForEvent:] + 1356
19  UIKit                               0x00b18770 -[UIWindow sendEvent:] + 770
20  UIKit                               0x00ad6681 -[UIApplication sendEvent:] + 242
21  UIKit                               0x00ae6ab8 _UIApplicationHandleEventFromQueueEvent + 21484
22  UIKit                               0x00aba2e7 _UIApplicationHandleEventQueue + 2300
23  CoreFoundation                      0x001bc06f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
24  CoreFoundation                      0x001b1b7d __CFRunLoopDoSources0 + 253
25  CoreFoundation                      0x001b10d8 __CFRunLoopRun + 952
26  CoreFoundation                      0x001b0a5b CFRunLoopRunSpecific + 443
27  CoreFoundation                      0x001b088b CFRunLoopRunInMode + 123
28  GraphicsServices                    0x03f3c2c9 GSEventRunModal + 192
29  GraphicsServices                    0x03f3c106 GSEventRun + 104
30  UIKit                               0x00abe0b6 UIApplicationMain + 1526
31  Shoppy                              0x0006e304 main + 180
32  libdyld.dylib                       0x0217bac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

我认为此错误消息显示是因为我已设置

var myarray = NSUserDefaults.standardUserDefaults().objectForKey("array") as! [String]

在另一个班级。也许这个值无法通过多个类访问。请帮助我!我是编程的新手:(

1 个答案:

答案 0 :(得分:1)

如果您尝试从NSUserDefaults中删除该阵列,则此行

self.DataFromOtherClass.removeObjectForKey("array")

应该是

NSUserDefaults.standardUserDefaults().removeObjectForKey("array")
NSUserDefaults.standardUserDefaults().synchronize()

错误告诉您,Array并不知道removeObjectForKey的含义,与NSDictionary

相关的错误

另请注意,如果您要更新NSUserDefaults,则需要synchronize

相关问题