如何在设备上编写plist?目标C iPhone

发布于 2024-10-09 17:17:26 字数 1931 浏览 2 评论 0原文

我有这样的代码:

#define ALERT(X)    {UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Info" message:X delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];[alert show];[alert release];}

- (id)readPlist:(NSString *)fileName {  
    NSData *plistData;  
    NSString *error;  
    NSPropertyListFormat format;  
    id plist;  

    NSString *localizedPath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"];  
    plistData = [NSData dataWithContentsOfFile:localizedPath];   

    plist = [NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error];  
    if (!plist) {  
        NSLog(@"Error reading plist from file '%s', error = '%s'", [localizedPath UTF8String], [error UTF8String]);  
        [error release];  
    }  

    return plist;  
}  

- (void)writeToPlist: (NSString*)a
{
    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSString *finalPath = [path stringByAppendingPathComponent:@"data.plist"];
    NSMutableArray* pArray = [[NSMutableArray alloc] initWithContentsOfFile:finalPath];

    [pArray addObject:a];
    [pArray writeToFile:finalPath atomically: YES];
    ALERT(@"finished");
    /* This would change the firmware version in the plist to 1.1.1 by initing the NSDictionary with the plist, then changing the value of the string in the key "ProductVersion" to what you specified */
} 

- (void)viewDidLoad {
    [super viewDidLoad];
    [self writeToPlist:@"this is a test"];
    NSArray* the = (NSArray*)[self readPlist:@"data"];
    NSString* s = [NSString stringWithFormat:@"%@",the];
    ALERT(s);
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

在模拟器上,第二个警报正确显示文件的内容,但在设备上它什么也没显示?我做错了什么?请显示代码/片段......

I have this code:

#define ALERT(X)    {UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Info" message:X delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];[alert show];[alert release];}

- (id)readPlist:(NSString *)fileName {  
    NSData *plistData;  
    NSString *error;  
    NSPropertyListFormat format;  
    id plist;  

    NSString *localizedPath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"];  
    plistData = [NSData dataWithContentsOfFile:localizedPath];   

    plist = [NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error];  
    if (!plist) {  
        NSLog(@"Error reading plist from file '%s', error = '%s'", [localizedPath UTF8String], [error UTF8String]);  
        [error release];  
    }  

    return plist;  
}  

- (void)writeToPlist: (NSString*)a
{
    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSString *finalPath = [path stringByAppendingPathComponent:@"data.plist"];
    NSMutableArray* pArray = [[NSMutableArray alloc] initWithContentsOfFile:finalPath];

    [pArray addObject:a];
    [pArray writeToFile:finalPath atomically: YES];
    ALERT(@"finished");
    /* This would change the firmware version in the plist to 1.1.1 by initing the NSDictionary with the plist, then changing the value of the string in the key "ProductVersion" to what you specified */
} 

- (void)viewDidLoad {
    [super viewDidLoad];
    [self writeToPlist:@"this is a test"];
    NSArray* the = (NSArray*)[self readPlist:@"data"];
    NSString* s = [NSString stringWithFormat:@"%@",the];
    ALERT(s);
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

On the simulator the second alert shows the content of the file correctly but on the device it shows nothing?? What's i'm doing wrong? Please show a code/snippet....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫腻 2024-10-16 17:17:27

问题是您无法将文件写入 mainBundle 文件夹,您的设备上只能访问 Documents 文件夹。

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

The problem is that you could not write a file into mainBundle folder, only Documents folder is accessable on your Device.

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文