2012-09-08 10 views
7

संभव डुप्लिकेट:
Send and receive NSData via GameKitएनएसडीटा में संरचना कैसे पैक करें?

मैं struct पूर्णांक चर और 2 नाव संकेत (सरणियों) के होते हैं जो की है। मैं इस संरचना ib NSData को कैसे पैक कर सकता हूं और बाद में इसे अनपैक कर सकता हूं?

+1

उपयोग [ 'dataWithBytes'] (http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation /Classes/NSData_Class/Reference/Reference.html#//apple_ref/occ/clm/NSData/dataWithBytes:length :) और आपूर्ति '(कॉन्स शून्य *) और struct_data' और' sizeof struct_data' – oldrinb

उत्तर

10

आप dataWithBytes का उपयोग कर संरचना पैक कर सकते हैं विधि पीएफ NSData:

struct aStruct { 
/* Implementation */ 
}; 

//Struct variable 
aStruct exampleStruct; 

// pack the struct into an NSData Object 
NSData *myData = [NSData dataWithBytes:&exampleStruct length:sizeof(exampleStruct)]; 

// get back the the struct from the object 
[myData getBytes:&exampleStruct length:sizeof(exampleStruct)]; 
+0

धन्यवाद! क्या आप यह भी दिखा सकते हैं कि फ्लोट * एक्स और फ्लोट * वाई संरचना में हैं, उन्हें कैसे प्रारंभ करें, संख्याएं जोड़ें और बाद में उन्हें अनपॅक किए गए ढांचे से प्राप्त करें? Coz मेरी प्राप्ति काम नहीं करती है और मुझे एरे में एक और संख्या मिलती है, न कि मैं – Mathemage

+2

@ वैलेंन्टिन क्रहरुलकोव में डालता हूं यदि ये structs डेटा का प्रतिनिधित्व करते हैं जिन्हें चलने वाले अनुप्रयोग के संदर्भ के बाहर क्रमबद्ध और जारी रखा जाना चाहिए तो मुझे नहीं लगता कि यह काम करेगा आपके लिए पॉइंटर्स उनके अंदर अच्छा है। –

+0

@ करल Veazey nono, ऐप के अंदर, बस इसे कैसे करें? – Mathemage

संबंधित मुद्दे