Swift5.0的Data类型

二进制是计算机的基本进制,存储在硬盘、内存包括网络传输的数据都是以二进制存在。在实际开发中,经常会碰到普通数据与二进制数据的互换。
Swift 用Data表示二进制数据,同样也是一个结构体。
从 Swift 5 开始,Data只用于表达连续的数据。Apple 引入了ContiguousBytes协议来表示数据的连续。遵守这个协议表示该类型提供以连续的方式直接访问底层原始数据的能力。以后不需要担心有时内存占用会莫名变大的问题了。

字符串与Data的转换

var send: String  = "hello"
let data: Data = send.data(using: .utf8)!

let receive: String = String(data: data, encoding: .utf8)!
print(receive)

Int与Data的转换

var send1: Int = 199
var data1: Data = Data(bytes: &send1, count: 8)

var receive1: Int = 0
(data1 as NSData).getBytes(&receive1, length: 8)
print(receive1)
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Learn Swift by Building Applications: Explore Swift programming through iOS app development by Emil Atanasov Packt Publishing English 2018-05-25 366 pages 5.0/5.0 1 reviews Details Title: Learn Swift by Building Applications: Explore Swift programming through iOS app development Author: Emil Atanasov Length: 366 pages Edition: 1 Language: English Publisher: Packt Publishing Publication Date: 2018-05-25 ISBN-10: 178646392X ISBN-13: 9781786463920 Sales Rank: #1123253 (See Top 100 Books) Categories Computers & Technology Mobile Phones, Tablets & E-Readers Operating Systems Programming Programming Languages Description Start building your very own mobile apps with this comprehensive introduction to Swift and object-oriented programming Key Features A complete beginner's guide to Swift programming language Understand core Swift programming concepts and techniques for creating popular iOS apps Start your journey toward building mobile app development with this practical guide Book Description Swift Language is now more powerful than ever; it has introduced new ways to solve old problems and has gone on to become one of the fastest growing popular languages. It is now a de-facto choice for iOS developers and it powers most of the newly released and popular apps. This practical guide will help you to begin your journey with Swift programming through learning how to build iOS apps. You will learn all about basic variables, if clauses, functions, loops, and other core concepts; then structures, classes, and inheritance will be discussed. Next, you'll dive into developing a weather app that consumes data from the internet and presents information to the user. The final project is more complex, involving creating an Instagram like app that integrates different external libraries. The app also uses CocoaPods as its package dependency manager, to give you a cutting-edge tool to add to your skillset. By the end of the book, you will have learned how to model real-world apps

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值