NSDictionary和NSData之間的相互轉換,nsdictionarynsdata

來源:互聯網
上載者:User

NSDictionary和NSData之間的相互轉換,nsdictionarynsdata

我封裝好了2個方法,用來字典和data相互轉換的,直接複製方法拿來用就可以了,注意2點:

1.我是用的mrc,用arc的同學把autorelease去掉即可。

2.第二個我是路徑檔案轉的字典,想data轉的話自己把方法改一下就行了。


//字典轉data

+(NSData *)returnDataWithDictionary:(NSDictionary *)dict

{

    NSMutableData * data = [[NSMutableDataalloc] init];

    NSKeyedArchiver * archiver = [[NSKeyedArchiveralloc] initForWritingWithMutableData:data];

    [archiverencodeObject:dict forKey:@"talkData"];

    [archiverfinishEncoding];


    [dataautorelease];

    [archiverautorelease];

    

   return data;

}

//路徑檔案轉dictonary

+(NSDictionary *)returnDictionaryWithDataPath:(NSString *)path

{

    NSData * data = [[NSMutableDataalloc] initWithContentsOfFile:path];

    NSKeyedUnarchiver * unarchiver = [[NSKeyedUnarchiveralloc] initForReadingWithData:data];

   NSDictionary * myDictionary = [[unarchiver decodeObjectForKey:@"talkData"] retain];

    [unarchiverfinishDecoding];

//    NSLog(@"%@", myDictionary);

    [unarchiverautorelease];

    [dataautorelease];

  

   return myDictionary;

}


IPHONE開發怎串連伺服器端?

伺服器端用Tomcat提供服務。在用戶端和伺服器端之間通過JSON,將常用資料類型(NSArray、NSData、NSDictionary等)和特定格式的字串相互轉換,實現用戶端和伺服器端的資訊傳遞。
 
C# 手機伺服器端開發

.net 伺服器端用iis提供服務用戶端和伺服器端通過表單傳值(post 和 get)通訊,也可以通過別的方法通訊,伺服器之間用web servers或者別的借口通訊。iphone開發的話和c#沒聯絡啊,蘋果有一套他自己的開發語言,如果要學習蘋果開發的話就需要學習他的語言和平台
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.