草庐IT

ios - 将自定义类对象转换为 NSData

我有一个自定义类,我想将其保存到NSUserDefaults中。我被告知我需要将类对象转换为数据,以便将其保存到NSUserDefaults。我发现了很多离散字符串或整数到NSData的例子,但没有关于NSData的自定义类。我对NSData编码等的复杂性知之甚少。感谢任何帮助编辑:虽然我知道这里有类似的答案,但它们都不在Swift中。语言之间的翻译是可行的,但它非常乏味,有时甚至非常违反直觉。 最佳答案 这是一个简单的例子://Customclass.classPerson:NSObject,NSCoding{varname:St

swift - 在 Swift 中打印 NSData?

如何在Swift中输出NSData的值?是NSLog、print还是println?看来我只能打印对象的地址,而不是它的实际内容。谢谢 最佳答案 因此,在尝试了网站上的一些建议之后,这就是最终对我有用的东西。swift3letstring1=String(data:jsonData,encoding:String.Encoding.utf8)??"Datacouldnotbeprinted"print(string1)swift1letstring1=NSString(data:jsonData,encoding:NSUTF8Str

swift - 在 Swift 中打印 NSData?

如何在Swift中输出NSData的值?是NSLog、print还是println?看来我只能打印对象的地址,而不是它的实际内容。谢谢 最佳答案 因此,在尝试了网站上的一些建议之后,这就是最终对我有用的东西。swift3letstring1=String(data:jsonData,encoding:String.Encoding.utf8)??"Datacouldnotbeprinted"print(string1)swift1letstring1=NSString(data:jsonData,encoding:NSUTF8Str

ios - 从 (_,_,_) 类型的抛出函数 throws -> Void 到非抛出函数类型 (NSData?, NSURLResponse?, NSError?) -> Void 的无效转换

我写了这段代码:funcgetjson(){leturlPath="https://api.whitehouse.gov/v1/petitions.json?limit=100"leturl=NSURL(string:urlPath)letsession=NSURLSession.sharedSession()lettask=session.dataTaskWithURL(url!,completionHandler:{data,response,error->Voidinprint("Taskcompleted")if(error!=nil){print(error!.localiz

ios - 从 (_,_,_) 类型的抛出函数 throws -> Void 到非抛出函数类型 (NSData?, NSURLResponse?, NSError?) -> Void 的无效转换

我写了这段代码:funcgetjson(){leturlPath="https://api.whitehouse.gov/v1/petitions.json?limit=100"leturl=NSURL(string:urlPath)letsession=NSURLSession.sharedSession()lettask=session.dataTaskWithURL(url!,completionHandler:{data,response,error->Voidinprint("Taskcompleted")if(error!=nil){print(error!.localiz

swift - Swift中如何将String转为byte?

Swift中如何将String转byte?类似于Java中的String.getBytes()。 最佳答案 还有一种更优雅的方式。swift3:letstr="Hello"letbuf=[UInt8](str.utf8)Swift4:(感谢@PJ_Finnegan)letstr="Hello"letbuf:[UInt8]=Array(str.utf8) 关于swift-Swift中如何将String转为byte?,我们在StackOverflow上找到一个类似的问题:

swift - Swift中如何将String转为byte?

Swift中如何将String转byte?类似于Java中的String.getBytes()。 最佳答案 还有一种更优雅的方式。swift3:letstr="Hello"letbuf=[UInt8](str.utf8)Swift4:(感谢@PJ_Finnegan)letstr="Hello"letbuf:[UInt8]=Array(str.utf8) 关于swift-Swift中如何将String转为byte?,我们在StackOverflow上找到一个类似的问题:

ios - 在 Swift 中将十六进制字符串转换为 NSData

我得到了在Objective-C中将字符串转换为十六进制字符串的代码:-(NSString*)CreateDataWithHexString:(NSString*)inputString{NSUIntegerinLength=[inputStringlength];unichar*inCharacters=alloca(sizeof(unichar)*inLength);[inputStringgetCharacters:inCharactersrange:NSMakeRange(0,inLength)];UInt8*outBytes=malloc(sizeof(UInt8)*((in

ios - 在 Swift 中将十六进制字符串转换为 NSData

我得到了在Objective-C中将字符串转换为十六进制字符串的代码:-(NSString*)CreateDataWithHexString:(NSString*)inputString{NSUIntegerinLength=[inputStringlength];unichar*inCharacters=alloca(sizeof(unichar)*inLength);[inputStringgetCharacters:inCharactersrange:NSMakeRange(0,inLength)];UInt8*outBytes=malloc(sizeof(UInt8)*((in

ios - Swift 中来自字节数组的 NSData

我正在尝试从一个字节数组创建一个NSDatavar。在Obj-C中我可能会这样做:NSData*endMarker=[[NSDataalloc]initWithBytes:{0xFF,0xD9},长度:2]我想不出Swift中的工作等价物。 最佳答案 NSData有一个接受bytes的初始化器指针:init(bytes:UnsafeMutablePointer,length:Int).一个UnsafePointerparameter可以接受各种不同的东西,包括一个简单的Swift数组,所以你可以使用与Objective-C中几乎相同