2013-10-24 31 views
0

我正在使用單例類(contactStorage)和數據模型(contactModel)來存儲聯繫人列表。我在我的根視圖控制器的viewdidload中創建了一個聯繫對象,並嘗試將其添加到NSMutableArray,但它不會「粘住」。我在addContact過程中記錄了傳入對象,並且它會生成準確的輸出,但是,addObject:c不會將其添加到數組中。對此有何看法?Singleton Array不會存儲數據模型

#import "contactListViewController.h" 
#import "contactDetailScreenViewController.h" 
#import "ContactModel.h" 
#import "contactStorage.h" 

@interface contactListViewController() 

@end 

@implementation contactListViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    ContactModel* c = [[ContactModel alloc] initWithfName:@"Mike" andlName:@"Deasy" andEmail:@"[email protected]" andPhone:@"4127154194"]; 
    [c logContact]; 


    [[contactStorage shared]addContact:c]; 
    [[contactStorage shared]saveToFile]; 
    [c release]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


@end 

我單身代碼:

// 
// contactStorage.m 
// contactList 
// 
// Created by dev on 10/23/13. 
// Copyright (c) 2013 Deasy, Michael William. All rights reserved. 
// 

#import "contactStorage.h" 

@implementation contactStorage 
{ 

} 

@synthesize cList = _cList; 

static contactStorage* _myOnlyInstance = nil; 
#pragma mark Storage Methods 

-(void)addContact: (ContactModel*) c 
{ 
    [c logContact]; 
    [self.cList addObject:c]; 
    NSLog(@"%@", _cList); 
} 


-(ContactModel*)getContact: (NSIndexPath*) index 
{ 
    return [self.cList objectAtIndex:index.row]; 
} 

-(NSMutableArray*)deleteContact: (NSIndexPath*) index 
{ 
    [self.cList removeObjectAtIndex:index.row]; 
    return self.cList; 
} 

-(NSMutableArray*)getAllContacts 
{ 
    return self.cList; 
} 


-(void)saveToFile 
{ 
    NSString* path = [[self documentsPath] stringByAppendingPathComponent:@"data.txt"]; 
    NSLog(@"%@",path); 
    [_cList writeToFile:path atomically:YES]; 
    NSLog(@"%@", self.cList); 
} 

#pragma mark Singleton Create 

-(id)init 
{ 
    self = [super init]; 
    if (self) 
    { 
     NSLog(@"Initing the array"); 
     _cList = [[NSMutableArray alloc] init]; 
    } 
    return self; 
} 

+(contactStorage*)shared 
{ 
    if (_myOnlyInstance == nil) 
    { 
     _myOnlyInstance = [[contactStorage alloc] init]; 
    } 
    return _myOnlyInstance; 
} 

-(NSString*) documentsPath 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir = [paths objectAtIndex:0]; 
    return documentsDir; 
} 

@end 

我contactModel代碼:

// 
// ContactModel.m 
// contactList 
// 
// Created by dev on 10/23/13. 
// Copyright (c) 2013 Deasy, Michael William. All rights reserved. 
// 

#import "ContactModel.h" 

@implementation ContactModel 
{ 

} 

@synthesize fName = _fName; 
@synthesize lName = _lName; 
@synthesize email = _email; 
@synthesize phone = _phone; 

-(void)logContact 
{ 
    NSLog(@"%@", self.fName); 
    NSLog(@"%@", self.lName); 
    NSLog(@"%@", self.email); 
    NSLog(@"%@", self.phone); 
} 

-(void)dealloc 
{ 
    [_fName release]; 
    [_lName release]; 
    [_email release]; 
    [_phone release]; 
    [super dealloc]; 
} 

-(id) initWithfName: (NSString*) fName 
      andlName: (NSString*) lName 
      andEmail: (NSString*) email 
      andPhone: (NSString*) phone 
{ 
    self = [super init]; 
    _fName = [[NSString alloc] initWithString:fName]; 
    _lName = [[NSString alloc] initWithString:lName]; 
    _email = [[NSString alloc] initWithString:email]; 
    _phone = [[NSString alloc] initWithString:phone]; 
    return self; 
} 

@end 

的NSLog輸出:

2013-10-24 12:50:35.573 contactList[3097:a0b] Mike 
2013-10-24 12:50:35.574 contactList[3097:a0b] Deasy 
2013-10-24 12:50:35.575 contactList[3097:a0b] [email protected] 
2013-10-24 12:50:35.575 contactList[3097:a0b] 4127154194 
2013-10-24 12:50:35.576 contactList[3097:a0b] Initing the array 
2013-10-24 12:50:35.576 contactList[3097:a0b] Mike 
2013-10-24 12:50:35.576 contactList[3097:a0b] Deasy 
2013-10-24 12:50:35.577 contactList[3097:a0b] [email protected] 
2013-10-24 12:50:35.577 contactList[3097:a0b] 4127154194 
2013-10-24 12:50:35.578 contactList[3097:a0b] (
    "<ContactModel: 0x8d72720>" 
) 
2013-10-24 12:50:35.578 contactList[3097:a0b] /Users/dev/Library/Application Support/iPhone Simulator/7.0/Applications/7CFD98F0-C502-49E5-953B-FD43B61EDC38/Documents/data.txt 
2013-10-24 12:50:35.579 contactList[3097:a0b] (
    "<ContactModel: 0x8d72720>" 
) 
+0

什麼使你相信'ContactModel'沒有被添加到數組中?將數組寫入磁盤可能存在問題:https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/doc/uid/ 20000137-BABCGEFF –

+0

(類名通常在obj-c中大寫) – nielsbot

回答

2

顯然,你的單身成功加入ContactModel對象到您的單例數組(如您的NSLog聲明所證明的)。我假設你的問題源於你沒有看到你的文件被保存的事實。

這是因爲您正在嘗試使用NSMutableArray(試圖保存plist文件)的writeToFile。如果您檢查writeToFile的退貨代碼,您將看到它失敗。這是因爲你不能用一個由自定義對象組成的數組寫一個plist。你可能想使用NSKeyedArchiver代替,例如:

- (void)saveToFile 
{ 
    NSString* path = [[self documentsPath] stringByAppendingPathComponent:@"cList.dat"]; 
    BOOL success = [NSKeyedArchiver archiveRootObject:_cList toFile:path]; 
    NSAssert(success, @"write failed"); 
} 

預見的合乎邏輯的後續問題,如何讀取這個文件,你會使用NSKeyedUnarchiver,就像這樣:

-(void)loadFromFile 
{ 
    NSString* path = [[self documentsPath] stringByAppendingPathComponent:@"cList.dat"]; 
    self.cList = [NSKeyedUnarchiver unarchiveObjectWithFile:path]; 
    NSAssert(_cList, @"read failed"); 
} 

但是,對於這個工作,你必須使你接觸模型符合NSCoding協議,即添加下面的方法到類:

#pragma mark - NSCoding methods 

- (NSArray *)propertyNames 
{ 
    return @[@"fName", @"lName", @"email", @"phone"]; 
} 

- (id) initWithCoder:(NSCoder *)aDecoder 
{ 
    // if `super` conforms to `NSCoding`, then use 
    // 
    // self = [super initWithCoder:aDecoder]; 
    // 
    // in this case, `super` is `NSObject`, so just call `init` 

    self = [super init]; 

    if (self) { 
     for (NSString *key in [self propertyNames]) { 
      [self setValue:[aDecoder decodeObjectForKey:key] forKey:key]; 
     } 
    } 

    return self; 
} 

- (void)encodeWithCoder:(NSCoder *)aCoder 
{ 
    // if `super` conforms to `NSCoding`, itself, then call `encodeWithCoder` for `super`: 
    // 
    // [super encodeWithCoder:aCoder]; 
    // 
    // in this case, `super` is `NSObject`, so that is not needed 

    for (NSString *key in [self propertyNames]) { 
     [aCoder encodeObject:[self valueForKey:key] forKey:key]; 
    } 
} 

有關使用檔案的更多信息,請參閱Archives and Serializations Programming Guide

+0

完美的男人,謝謝。 –

+0

@MikeD。大。順便說一句,如果我回答了您的問題,您可以考慮通過點擊我答案旁邊的複選標記來接受它。請參閱[當某人回答我的問題時該怎麼辦?](http://meta.stackoverflow.com/help/someone-answers)顯然,如果我沒有回答它(或者您仍然在努力),沒關係,你不必接受我的回答。但這只是一個關閉「開放」問題的技巧。 – Rob