我試圖存儲幾個字符串在我的... NSUserDefaults的想不通我做錯了什麼......不會儲存我的NSString的
-(IBAction)addToFavorite:(id)sender {
NSMutableArray* favoritedAlready = [[[NSUserDefaults standardUserDefaults] objectForKey:@"favorites"] mutableCopy];
NSLog(@"%@",favoritedAlready);
if (![favoritedAlready containsObject:indexPathRowString]) {
[favoritedAlready addObject:indexPathRowString];
[[NSUserDefaults standardUserDefaults] setObject:favoritedAlready forKey:@"favorites"];
[[NSUserDefaults standardUserDefaults] synchronize];
favoritMarkerad.image = [UIImage imageNamed:@"StarFilled.png"];}
else {
[favoritedAlready removeObject:indexPathRowString];
[[NSUserDefaults standardUserDefaults] setObject:favoritedAlready forKey:@"favorites"];
[[NSUserDefaults standardUserDefaults] synchronize];
favoritMarkerad.image = [UIImage imageNamed:@"StarEmpty.png"];
}
當我嘗試載入我的陣列中的另一個視圖 - 控制沒有任何反應..
(.h)
NSMutableArray *favorites_;
@property (nonatomic, retain) NSMutableArray *favorites_;
(.m)
@synthesize favorites_;
-(void)makeData{
NSMutableArray* favoritedAlready = [[NSUserDefaults standardUserDefaults] objectForKey:@"favorites"];
[[NSUserDefaults standardUserDefaults] synchronize];
favorites_ = favoritedAlready;}
「的NSLog(@」 %@ 「favoritedAlready);」給出「2012-01-25 14:53:26.360 myApp [2752:1be03](null)」 有什麼想法?
什麼DOE你當你'的NSLog(@「% @「,favoritedAlready);'在'mutableCopy'方法之後? 「 – basvk
」2012-01-25 14:52:07.385 myapplication [2714:1be03](null)「 – Christoffer
您將字符串放入數組中並將其存儲並且您的邏輯似乎是,如果字符串尚未存在那麼你添加它,如果它已經存在,那麼你刪除它?這一切是否準確?就產量而言,當你期望時,你大概不會看到「新增喜愛」?你有沒有嘗試NSLogging實際的數組看看有什麼在那裏作爲診斷步驟? – Tommy