2016-08-19 27 views
0

私は内部にIDを持っていますが、毎回データを要求したくないので、plistとしてwriteToFileを書きたいと思っていますが、最初に書かれたものはあまりに遅く、メモリが急騰しています。 は、私は、ユーザーが再び.plistファイルを作成する方法

を記述する必要はありませんので、このようにして得られたplistファイルがXcodeのビューには何のデータではありませんが、実際にはそれが既に持っている、ローカルファイルとしてのplistにそれを作るために使用することができます任意の方法はあります書かれて、あなたは私が後でuは国のエンドポイントを要求したとき、それはリストにそれを見つけた場合、それは、キャッシュに定義されたエンドポイントをチェックし、それが存在する、APIのレスポンスをキャッシュするクラスを書いたコード

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); 
NSString *plistPath = [paths objectAtIndex:0]; 
NSMutableArray *marr = [NSMutableArray array]; 
NSString *filename=[plistPath stringByAppendingPathComponent:@"city.plist"]; 
NSLog(@"filename == %@",filename); 
[marr addObject:@"字符串"]; 
[marr writeToFile:filename atomically:YES]; 
+0

あなたがAppleのやり方を試してみました急騰メモリにつながる、私は以上の2000のキーと値 – vadian

答えて

0

を介してデータを見つけることができますキャッシュ内にキャッシュされたものが返され、HTTPリクエストの実行が完了しない場合は、ドキュメントの実装方法の手順が説明されています。

MGCacheManager

+0

を見てみましょうそれをするの?同様のエラーが表示される場合は、ロジックに何か間違いがあるはずです。 – BaQiWL

0

次の関数の入力としてserilizableオブジェクト(辞書、配列)を取り、PLISTに変換し、アプリケーションのドキュメントdisrectoryでそれを書く:あなたが目的でプロパティリストを作成することができます

+ (BOOL) writeBundelPlist : (NSString *) plistName with : (id) newPlistData { 
    NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:plistName]; 
    if (![fileManager fileExistsAtPath:plistPath]) { 
     NSString *bundle = [[NSBundle mainBundle] pathForResource:[plistName stringByDeletingPathExtension] ofType:[plistName pathExtension]]; 
     NSLog(@"plistPath = %@", plistPath); 
     [fileManager copyItemAtPath:bundle toPath:plistPath error:&error]; 
    } 
    return [newPlistData writeToFile:plistPath atomically:YES]; 
} 
0

-C集約内のすべてのオブジェクトがNSDictionary、NSArray、NSString、NSDate、NSData、またはNSNumberクラスから派生している場合。

利用コード以下:詳細については

//Get the documents directory path 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"plist.plist"]; 
NSFileManager *fileManager = [NSFileManager defaultManager]; 

if (![fileManager fileExistsAtPath: path]) { 

    path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat:@"plist.plist"] ]; 
} 

NSMutableDictionary *data; 

if ([fileManager fileExistsAtPath: path]) { 

    data = [[NSMutableDictionary alloc] initWithContentsOfFile: path]; 
} 
else { 
    // If the file doesn’t exist, create an empty dictionary 
    data = [[NSMutableDictionary alloc] init]; 
} 

//To insert the data into the plist 
[data setObject:@"iPhone 6 Plus" forKey:@"value"]; 
[data writeToFile:path atomically:YES]; 

//To retrieve the data from the plist 
NSMutableDictionary *savedValue = [[NSMutableDictionary alloc] initWithContentsOfFile: path]; 
NSString *value = [savedValue objectForKey:@"value"]; 
NSLog(@"%@",value); 

here

をクリックしてAppleはまた、plistファイルhereを作成するためのデモプロジェクトを入れています。

+0

をありがとう、私はそれをしようとしますOK 'NSPropertyListSerialization'クラス – BaQiWL

+0

を記述する必要が、私は前にこのアプローチを試みたあなたに –

+0

NSPropertyListSerializationこのクラスは私の問題を解決します、ありがとう – BaQiWL

0

このメソッドは、evervoneに感謝非常に便利です、rmaddy

ためのおかげでNSPropertyListSerialization.dataWithPropertyList(NSMutableDictionary、フォーマットとしてresponse.result.valueしてみてください= { LETデータの操作を行います!NSPropertyListFormat.XMLFormat_v1_0、オプション:0 )

     data.writeToFile(countryListPath, atomically: true) 
        }catch{ 

        } 
1

あなたは、プログラムなしでPLISTを作成しようとしている場合は、次の手順を実行します。

  1. R 「ファイル」をクリックし、「新規ファイル...」オプションを選択します。
  2. [OS X]タブから[リソース]を選択します。
  3. プロパティリストのオプションが利用できます。
  4. 適切な名前を選択してください。

これがプロジェクトに追加されます。

関連する問題