Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 需要将表视图的内容保存为txt文件,然后重新加载_Objective C_Xcode_Macos_Reload_Save - Fatal编程技术网

Objective c 需要将表视图的内容保存为txt文件,然后重新加载

Objective c 需要将表视图的内容保存为txt文件,然后重新加载,objective-c,xcode,macos,reload,save,Objective C,Xcode,Macos,Reload,Save,我需要能够将由NSMutableArray提供数据的表视图的内容保存到txt文件中,然后需要在包含表视图的窗口打开时自动重新打开该文件。我正在申请mac 谢谢 这是数据源的代码: #import "tableViewData.h" #import "Customer.h" @implementation tableViewData -(id) init { self = [super init]; if (self) { list = nil; filepath = @"/Use

我需要能够将由NSMutableArray提供数据的表视图的内容保存到txt文件中,然后需要在包含表视图的窗口打开时自动重新打开该文件。我正在申请mac
谢谢 这是数据源的代码:

#import "tableViewData.h"
#import "Customer.h"
@implementation tableViewData
-(id) init
{
self = [super init];
if (self) {
    list = nil;
    filepath = @"/Users/Gautambir/Desktop/CustomerNames.txt";
    if ([[NSFileManager defaultManager]fileExistsAtPath:filepath]) {
        list = [[NSMutableArray alloc] initWithContentsOfFile:filepath];
    }
    else
    list = [[NSMutableArray alloc]initWithObjects:name,memberNumber ,nil];
        [list writeToFile:filepath atomically:YES];
}
return self;
}

-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{
return [list count];
}

-(id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn     *)tableColumn row:(NSInteger)row{
Customer *Customer = [list objectAtIndex:row];
NSString *identifier = [tableColumn identifier];
return [Customer valueForKey:identifier];
}

-(void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
Customer *Customer = [list objectAtIndex:row];
NSString *identifier = [tableColumn identifier];
[Customer setValue:object forKey:identifier];
}


-(void)save{
[list writeToFile:filepath atomically:YES];
}
-(IBAction)add:(id)sender{
[list addObject:[[Customer alloc]init]];
[tableView reloadData];

NSLog (@"array:%@",list);


}

-(IBAction)remove:(id)sender{
NSInteger row = [tableView selectedRow];
if (row != -1) {
    [list removeObjectAtIndex:row];
}

[tableView reloadData];

}




-(void)dealloc
{
[super dealloc];
}


@end
这是customer对象类的.m文件:

#import "Customer.h"

@implementation Customer

@synthesize name;
@synthesize memberNumber;


-(id) init
{
self = [super init];
if(self) {
    name = @"Test";
    int i = arc4random()%1000000000000000000;
    if (i<0) {
        memberNumber = i*-1;
    }
    else
        memberNumber = i;
}
return self;

}

-(void)dealloc
{
[name release];
[super dealloc];
}

-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self)
{
    name = [[aDecoder decodeObjectForKey:@"name"]retain];
    memberNumber = [aDecoder decodeIntForKey:@"memberNumeber"];
}
return self;

}

-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:name forKey:@"name"];
[aCoder encodeInt:memberNumber forKey:@"memberNumber"];
}

@end
#导入“Customer.h”
@实施客户
@综合名称;
@综合成员数;
-(id)init
{
self=[super init];
如果(自我){
名称=@“测试”;
int i=arc4random()%10000000000000000;

如果(我很抱歉发布另一个答案-我误读了标签,我的第一个答案是依靠iOS。 这是在OSX中执行此操作的方法:

保存

NSMutableArray *array = ...  //your array

[array addObject:...]; 
[array addObject:...];
[array addObject:...];
...

// then use an NSData to store the array
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];
NSString *path = @"/Users/User/path...";
[data writeToFile:path options:NSDataWritingAtomic error:nil];
NSMutableArray *archive = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
检索

NSMutableArray *array = ...  //your array

[array addObject:...]; 
[array addObject:...];
[array addObject:...];
...

// then use an NSData to store the array
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];
NSString *path = @"/Users/User/path...";
[data writeToFile:path options:NSDataWritingAtomic error:nil];
NSMutableArray *archive = [NSKeyedUnarchiver unarchiveObjectWithFile:path];

塞巴斯蒂安

亲爱的塞巴斯蒂安。你真了不起,是个救命恩人。你可以想象我花了多长时间来努力让这项工作成功,这么多人都很粗鲁,不回答这个问题,但你真了不起,非常感谢你。我还有一个问题想问你,如果我想发言的话,你能正确回答问题我可以打开另一个窗口上的客户ID中的客户名称,请让我知道我该怎么做。再次非常感谢您Hi,首先:很高兴我能提供帮助,但是:在stackOverflow上,这是一个
规则
投票并接受您喜欢的答案;-)这将关闭问题并使其从流中消失。请这样做。对于任何其他问题,请打开一个新的问题,混合不同的问题是不好的做法,供可能在此处搜索特定内容的未来用户使用。