| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
shannon
8年前发布

Swift结构体指针操作

   <h2><strong>C语言的指针操作</strong></h2>    <p><strong>在c语言中申明一个变量并通过指针修改该变量的值</strong></p>    <pre>  <code class="language-swift">int a = 1;    int *p = &a;  *p = 2;    printf("a value is %d\\\\n",a);</code></pre>    <p><strong>a value is 2</strong></p>    <p><strong>c语言操作结构体指针操作</strong></p>    <ul>     <li> <p>申明一个叫User的结构体</p> </li>    </ul>    <pre>  <code class="language-swift">typedef struct User{      int ID;      int age;  } User;</code></pre>    <ul>     <li> <p>申明一个结构体User变量user 设置ID值为1 age值为18</p> </li>    </ul>    <pre>  <code class="language-swift">User user;  user.ID = 1;  user.age = 3;</code></pre>    <ul>     <li> <p>通过指针对变量user的值进行修改</p> </li>    </ul>    <pre>  <code class="language-swift">//申明一个结构体指针变量userpointer指向user的地址  User *userpointer = &user;    //修改ID的值为2  int  *idPointer = (int *)userpointer;  *idPointer = 2;    //获得属性age所在指针  int *agePointer = ++ idPointer;  //修改user的age值为4  *agePointer = 4;  printf("user ID = %d, age = %d", user.ID, user.age);</code></pre>    <p><strong>user ID = 2, age = 4</strong></p>    <h2><strong>Swift</strong></h2>    <p>Swift并不推荐对指针进行直接操作,  但仍提供了几种可以直接操作内存的指针类型,以下是c与Swift的语法对应表 ,使用 Type 做类型占用</p>    <p>对于返回值、变量和参数,使用一下对应表</p>    <table>     <thead>      <tr>       <th>C 语法</th>       <th>Swift 语法</th>      </tr>     </thead>     <tbody>      <tr>       <td>const Type *</td>       <td>UnsafePointer</td>      </tr>      <tr>       <td>Type *</td>       <td>UnsafeMutablePointer</td>      </tr>     </tbody>    </table>    <p>对于类,使用一下语法对应</p>    <table>     <thead>      <tr>       <th>C 语法</th>       <th>Swift 语法</th>      </tr>     </thead>     <tbody>      <tr>       <td>Type <em>const</em></td>       <td>UnsafePointer</td>      </tr>      <tr>       <td>Type <em>__strong</em></td>       <td>UnsafeMutablePointer</td>      </tr>      <tr>       <td>Type **</td>       <td>AutoreleasingUnsafeMutablePointer</td>      </tr>     </tbody>    </table>    <p>在Swift中无类型的指针,原始内存可以用UnsafeRawPointer 和UnsafeMutableRawPointer来表示</p>    <p>如果像不完整结构体的这样的c指针的值的类型无法用Swift来表示,则用OpaquePointer来表示</p>    <p><strong>接下来,进行Swift版结构体指针操</strong>作</p>    <p>申明User结构体</p>    <pre>  <code class="language-swift">struct User {      var ID: Int      var age: Int  }</code></pre>    <p>申明变量,并获取变量地址, 创建一个user变量 ID初始值问为1 age初始值为3</p>    <p>通过指针设置ID值为2 设置age值为3</p>    <pre>  <code class="language-swift">var user = User(ID: 1, age: 3)  let userPointer = withUnsafePointer(to: &user, {$0})//UnsafePointer                //打印user指针的值  print(userPointer.pointee)  //User(ID: 1, age: 3)    //获取user ID的指针  let userIDPointer = unsafeBitCast(userPointer, to: UnsafeMutablePointer               .self) //设置ID的值为2 userIDPointer.pointee = 2 print(userPointer.pointee) //User(ID: 2, age: 3) //获取user age的指针 let agePointer = userIDPointer.advanced(by: 1) agePointer.pointee = 4 print(userPointer.pointee) //User(ID: 2, age: 4)            </code></pre>    <p><strong>虽然c和Swift的语法差距有点大,但是原理是相同的</strong></p>    <p>接下来我们看一个更复杂一点的例子</p>    <pre>  <code class="language-swift">public struct Person {         var age: Int         var firstName: String         var lastName: String         var phoneNumber: PhoneNumber   }  public struct PhoneNumber {         var number: String         var type: String   }</code></pre>    <p>创建person变量 并通过指针操作 获取对应的属性值</p>    <pre>  <code class="language-swift">let phone = PhoneNumber(number: "186xxxxxxxx", type: "work")  var person = Person(age: 24, firstName: "Bing", lastName: "lin", phoneNumber: phone)  let rawPointer = withUnsafePointer(to: &person, { UnsafeRawPointer($0)})    let age = rawPointer.load(fromByteOffset: 0, as: Int.self)  let firstName = rawPointer.load(fromByteOffset: 8, as: String.self)  let lastName  = rawPointer.load(fromByteOffset: 32, as: String.self)  let phoneInfo = rawPointer.load(fromByteOffset: 56, as: PhoneNumber.self)    print("age: \\\\(age) firstName: \\\\(firstName) lastName: \\\\(lastName) , phoneNumber: \\\\(phoneInfo)")  //age: 24 firstName: Bing lastName: lin , phoneNumber: PhoneNumber(number: "186xxxxxxxx", type: "work")</code></pre>    <p>到这里,我们已经学会了 通过指针获取值,通过指针设置对应的值</p>    <p>接下来 就可以运用这些知识发挥自己的创造力了</p>    <p><strong>参考资料</strong></p>    <p><a href="/misc/goto?guid=4959721632613144169" rel="nofollow,noindex">结构体的内存空间分配原理</a></p>    <p><a href="/misc/goto?guid=4959721632704307919" rel="nofollow,noindex">Swift 中的指针使用</a></p>    <p>文章转自 <a href="/misc/goto?guid=4959721632787158299" rel="nofollow,noindex">冰琳92的简书</a></p>    <p> </p>    <p>来自:http://www.cocoachina.com/swift/20161020/17802.html</p>    <p> </p>    
 本文由用户 shannon 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1476951306355.html
指针 Swift Apple Swift开发