在Swift中将NSData转换为Integer

前端之家收集整理的这篇文章主要介绍了在Swift中将NSData转换为Integer前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在Objective-C中,代码看起来很喜欢这个并且工作得很完美,
NSInteger random = arc4random_uniform(99) + 1 
NSData *data = [NSData dataWithBytes:& random length: sizeof(random)];
int value = *(int*)([data bytes]);

怎么能在Swift中完成?

像这样:
var src: NSInteger = 2525
var out: NSInteger = 0

let data = NSData(bytes: &src,length: sizeof(NSInteger))
data.getBytes(&out,length: sizeof(NSInteger))
println(out) // ==> 2525
原文链接:https://www.f2er.com/swift/319382.html

猜你在找的Swift相关文章