English 中文(简体)
无法读/篡改数据以备在Pipe中存档。
原标题:unable to read/write data to .plist file in iPhone

i 是新开发商,即制作电子Pub读物,阅读电子Pub文档。

我在我的电话中写了标语,我想读一下我的名字,把数据写到我的名单上。

这里是我的法典。

<>strong> Carloic: first i amdowning an ePub file,ePub file will bedown to this path

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSLog(@"basePath=%@",basePath);

output :- =/Users/krunal/Library/Application Support/iPhone Simulator/5.1/Applications/6B7FCD58-EDF9-44F4-8B33-5F3542536F92/Documents

now, i want to write name of Downloaded .ePubfile into file into my .plist

code:

NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: basePath];

    [data setObject:[NSNumber numberWithInt:value] forKey:@"value"];

    [data writeToFile: plistPath atomically:YES];
    [data release];

i 试图这样做,但我无法在我的<编码>.plist文档中写。

<><>>> 任何帮助 将使用。

www.un.org/spanish/ecosoc 感谢:

最佳回答

页: 1

NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: plistPath];

?


未经测试的法典如下:

第1步:将档案复印件交给它。

NSError *error1;
BOOL resourcesAlreadyInDocumentsDirectory;
BOOL copied1;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath1 = [documentsDirectory stringByAppendingString:@"/epub.plist"];

resourcesAlreadyInDocumentsDirectory = [fileManager fileExistsAtPath:filePath1];

if(resourcesAlreadyInDocumentsDirectory == YES) {

} else {

    NSString *path1 = [[[NSBundle mainBundle] resourcePath] stringByAppendingFormat:@"/epub.plist"];
    copied1 = [fileManager copyItemAtPath:path1 toPath:filePath1 error:&error1];

    if (!copied1) {
        NSAssert1(0, @"Failed to copy epub.plist. Error %@", [error1 localizedDescription]);
    }
}

步骤2:

NSMutableDictionary* dict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath1]; 

第3步:标准数据

[dict setObject:[NSNumber numberWithInt:value] forKey:@"value"];
[dict writeToFile:path atomically:YES];
问题回答
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: basePath];

data is nil here, You should init it with:

NSMutableDictionary *data = [[NSMutableDictionary alloc] init];

我的回答更清楚:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
if ( basePath == nil ) {
     return
}
NSMutableDictionary *data = [[NSMutableDictionary alloc] init];
[data setObject:[NSNumber numberWithInt:value] forKey:@"value"];
NSString *plistPath = [NSString stringWithFormat:@"%@/name.plist", basePath];
[data writeToFile: plistPath atomically:YES];
[data release];

便于使用NSUserDefault,将数据保存到一个清单档案中,其编码如下:

- (void)setOverviewGroupInstrument:(BOOL)isGroupded {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:[Your array] forKey:[Your Key]];
[prefs synchronize];
}

之后,请:

- (NSMutableArray*)getOverviewInstrumentList {
    return [prefs objectForKey:[Your Key]];
}




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签