0

を保存したいのですNSArrayが、失敗します。なぜですか?

 -(void)SaveDataAll:(NSMutableArray *)dataToSave:(NSString *)name
    {  
            NSArray *array = [NSArray arrayWithArray:dataToSave];


            // save path
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                                 NSUserDomainMask, YES);

            if ([paths count] > 0)
            {
                // Array path
                NSString  *arrayPath = [[paths objectAtIndex:0]
                                        stringByAppendingPathComponent:name];

                NSLog(@"SaveDataAll   array====%@",array);
                 NSLog(@"SaveDataAll   dataToSave====%@",dataToSave);  
                // save array
                [array writeToFile:arrayPath atomically:YES];

                // getout
                NSArray *arrayFromFile = [NSArray arrayWithContentsOfFile:arrayPath];

                NSLog(@"SaveDataAll   ====%@",arrayFromFile);  
            }
    }

これは印刷された配列です。

2013-01-22 16:51:45.010 [49227:c07] SaveDataAll   array====(
    "\U4ee5\U8272\U5217\U548c\U4f0a\U6717\Uff1a\U4e00\U500b\U611b\U60c5\U6545\U4e8b\Uff1f",
    "\U53f2\U63d0\U82ac\Uff0e\U611b\U5ef8\U65af: \U4e00\U500b\U7236\U89aa\U8207\U5973\U5152\U7684\U9023\U7e6b\Uff0c\U4e00\U6b21\U4e00\U5f35\U7167\U7247",
    "Molly Crockett \U8ac7\U5c0f\U5fc3\U300c\U795e\U7d93\U80e1\U8aaa\U300d",
    "Maz Jobrani: \U7576\U4e00\U500b\U6c99\U70cf\U5730\U963f\U62c9\U4f2f\U4eba\U3001\U5370\U5ea6\U4eba\U3001\U548c\U4f0a\U6717\U4eba\U8d70\U9032\U5361\U9054\U7684\U9152\U5427....",
    "Ellen 't Hoen: \U85e5\U7269\U5c08\U5229\U806f\U76df\Uff0c\U62ef\U6551\U751f\U547d",
    "Nina Tandon\Uff1a \U7d44\U7e54\U5de5\U7a0b\U5b78\U662f\U91ab\U7642\U500b\U4eba\U5316\U7684\U9060\U666f?",
    "\U5091\U592b\Uff0e\U53f2\U5bc6\U65af\Uff1a\U5f9e\U76e3\U7344\U5b78\U5230\U7684\U7d93\U5546\U4e4b\U9053",
    "Bobby Ghosh: \U70ba\U4f55\U5168\U7403\U8056\U6230\U5df2\U7d93\U8f38\U4e86",
    "\U4fdd\U7f85\U00b7\U5361\U8fea\U5c3c\Uff08Paolo Cardini\Uff09\Uff1a \U5225\U518d\U4e00\U5fc3\U591a\U7528\U4e86\Uff01\U8a66\U8a66\U5c08\U5fc3\U4e00\U81f4\U5427\Uff01",
    "Munir Virani: \U6211\U5982\U4f55\U611b\U4e0a\U79bf\U9df9",
    "\U73cd\U59ae\Uff0e\U96ea\U666e\Uff1a\U7834\U788e\U7684\U8eab\U9ad4\U4e0d\U4ee3\U8868\U7834\U788e\U7684\U4eba",
    "Ernesto Sirolli: \U60f3\U8981\U5e6b\U52a9\U4eba\U55ce? \U9589\U4e0a\U5634\Uff0c\U7136\U5f8c\U807d\U5427!",
    "14:57",
    "03:38",
    "11:18",
    "07:11",
    "11:16",
    "06:19",
    "05:00",
    "16:31",
    "02:52",
    "06:41",
    "18:57",
    "17:09",
    "14:57",
    "03:38",
    "11:18",
    "07:11",
    "11:16",
    "06:19",
    "05:00",
    "16:31",
    "02:52",
    "06:41",
    "18:57",
    "17:09",
    "<UIImage: 0x95075d0>",
    "<UIImage: 0xa297da0>",
    "<UIImage: 0xa29c480>",
    "<UIImage: 0xa27bae0>",
    "<UIImage: 0xa29b5c0>",
    "<UIImage: 0x950c0d0>",
    "<UIImage: 0xa29fcb0>",
    "<UIImage: 0x950cee0>",
    "<UIImage: 0xa2a0310>",
    "<UIImage: 0x9509310>",
    "<UIImage: 0x95509a0>",
    "<UIImage: 0xa297fe0>",
    "/talks/lang/zh-tw/israel_and_iran_a_love_story.html",
    "/talks/lang/zh-tw/steven_addis_a_father_daughter_bond_one_photo_at_a_time.html",
    "/talks/lang/zh-tw/molly_crockett_beware_neuro_bunk.html",
    "/talks/lang/zh-tw/maz_jobrani_a_saudi_an_indian_and_an_iranian_walk_into_a_qatari_bar.html",
    "/talks/lang/zh-tw/ellen_t_hoen_pool_medical_patents_save_lives.html",
    "/talks/lang/zh-tw/nina_tandon_could_tissue_engineering_mean_personalized_medicine.html",
    "/talks/lang/zh-tw/jeff_smith_lessons_in_business_from_prison.html",
    "/talks/lang/zh-tw/bobby_ghosh_why_global_jihad_is_losing.html",
    "/talks/lang/zh-tw/paolo_cardini_forget_multitasking_try_monotasking.html",
    "/talks/lang/zh-tw/munir_virani_why_i_love_vultures.html",
    "/talks/lang/zh-tw/janine_shepherd_a_broken_body_isn_t_a_broken_person.html",
    "/talks/lang/zh-tw/ernesto_sirolli_want_to_help_someone_shut_up_and_listen.html"
)

しかし、NSLog(@"SaveDataAll ====%@",arrayFromFile); arrayFromFile は常に nil ですか?

4

3 に答える 3

2

アレイにも画像があります。そのため、自分の場所に直接保存することはできません。したがって、NSArrayをNSData形式に変換する必要があります。そして、そのデータをあなたの場所に保存します。

NSData *data=[NSKeyedArchiver archivedDataWithRootObject:yourarrayName];
                [data writeToFile:yourfullpath atomically:YES];

同様に、ファイルを取得するときは、次のコードを使用します。

NSData *NewData=[NSData dataWithContentsOfFile:yourfullpath];
NSArray *arrayNew=[NSKeyedUnarchiver unarchiveObjectWithData:NewData];
于 2013-01-22T10:07:42.873 に答える
2

配列に UIImage があります - それらは保存できません

于 2013-01-22T09:32:00.737 に答える
0

ありがとう

配列のため、UIImage保存できません

これは簡単な方法の 1 つです。この問題は、----" archivedDataWithRootObject" で解決できます-------配列のルート オブジェクトを取得できます。

-(void)SaveDataAll :(NSMutableArray *)dataToSave:(NSString *)name {

[self  Save_str:dataToSave:name];
 }

-(void)Save_str:(NSMutableArray *)dataToSave1:(NSString *)name {

// -(void)SaveData:(NSInteger)rowChoice
{



    //change from nsmutalblearray to nsarray
    NSArray *array = [NSArray arrayWithArray:dataToSave1];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask, YES);

    if ([paths count] > 0)
    {
        // Array的保存路径
        NSString  *arrayPath = [[paths objectAtIndex:0]
                                stringByAppendingPathComponent:name];


        NSData *data=[NSKeyedArchiver archivedDataWithRootObject:array];


        bool saveok=[data writeToFile:arrayPath atomically:YES];

        if (saveok) {
            NSLog(@"SaveDataAll   saveok====true");

        }
        else{
            NSLog(@"SaveDataAll   saveok====false");
        }

    }

}
 }
于 2013-02-26T13:25:46.527 に答える