0

私はiPhone開発者に不慣れで、ePubファイルを読み取るためのePubリーダーを作成しています。

iPhoneアプリにplistがあり、.plistファイルにデータを読み書きしたいのですが、問題が発生しています。

これが私のコードスニペットです、

ロジック:最初にePubファイルをダウンロードしていますが、.ePubファイルはこのパスにダウンロードされます

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

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

.ePub今、ダウンロードしたファイルの名前を自分のファイルに書き込みたい .plist

code:

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

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

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

これを試しましたが、ファイルに書き込めません.plist

すべてのヘルプが適用されます。

前もって感謝します !!

4

3 に答える 3

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

ここではデータはゼロです。次のように初期化する必要があります。

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];
于 2012-05-17T10:07:29.003 に答える
0

もしかして:

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];
于 2012-05-17T10:27:34.407 に答える
0

NSUserDefaultを使用する方が簡単です。データは、次のコードとしてplistファイルに保存されます。

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

次に、次のように読むことができます。

- (NSMutableArray*)getOverviewInstrumentList {
    return [prefs objectForKey:[Your Key]];
}
于 2012-05-17T11:12:09.657 に答える