【游戏客户端开发】 IOS开发——Swift和Objective-C交互时的一些注意点

来源:互联网 发布:蜂窝移动网络收费吗 编辑:程序博客网 时间:2024/06/05 01:01

IOS开发——Swift和Objective-C交互时的一些注意点

  1. Swift如何调用Objective-C代码或第三方库
  2. Swift的数组和字典是否只能存储同一类型的数据
  3. Swift中有没有类似于Objective-C中的id
  4. Swift中如何自省
  5. 在Swift中如何使用GCD
  6. Swfit中的Internationalization宏
  7. 仍需注意持有循环

1. Swift如何调用Objective-C代码或第三方库

当向Xcode中添加第一个.swift文件时,Xcode会提示你需要添加一个头文件,这个头文件可以让你将一些Objective-C头文件添加到其中,这样swift就可以调用其中声明的Objective-C类(并且不用额外再import),就如同使用系统类一样方便。

2. Swift的数组和字典是否只能存储同一类型的数据

虽然Swift强烈建议对数组和字典进行强类型化,也就是说只能存储同一种数据类型,但实际上仍然是可以存储多种数据类型的(注:这样可能会导致一些严重的问题),若要存储不同数据类型,需要将数据类型声明为AnyObject: 
如:

<code class="hljs cs has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> array:AnyObject[] = [<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"string"</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">11.1</span>,<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>]<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> pople:Dictionary<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">string</span>,AnyObject> = [<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>:<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"jack"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"age"</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">25</span>]</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

3. Swift中有没有类似于Objective-C中的id

当Objective-C API的返回值为id时,Swift用AnyObject代替。AnyObject代表了任意类的实例,此外还有一个Any对象,用来表示任何类型的实例(function除外)。

4. Swift中如何自省

在Objective-C中我们可以通过类似“if([obj isKindeOfClass:[Foo class]]){…}”这样的方法来进行自省。 
同理,在Swift中也保留了类似这样的功能。Swift用is关键字检查变量/常量的类型,由于Swift是类型安全的,因此你根本不能对同一变量/常量进行类型不同的赋值。而且编译器会自动检查你的is语句是否是合法的。 
使用is进行自省的变量在声明时不可用显式声明去指定其类型,更不可直接赋值,如:

<code class="hljs r has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">var someValue = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"String"</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> someValue is String{<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">...</span>}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

这样子的写法在编译时就会报错,因为if someValue is String{…}这个判断一直为true,修改为正确的写法入下:

<code class="hljs r has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">var someValue:Any?someValue = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"String"</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> someValue is String{<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">...</span>}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

5. 在Swift中如何使用GCD

GCD——Grand Central Disptach,实际上是C API而非Objective-C的API,所以在Swift中使用GCD和在Objective-C中其实是一样的,也可以用NSOperationQueue进行异步处理。

<code class="hljs scss has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-function" style="box-sizing: border-box;">dispatch_async(<span class="hljs-function" style="box-sizing: border-box;">disptach_get_global_queue(DISPTACH_QUEUE_PRIORITY_BACKGROUND)</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>)</span>,{    <span class="hljs-function" style="box-sizing: border-box;">print(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"test"</span>)</span>});</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

6. Swfit中的Internationalization宏

Swift提供了自己的类似于Objective-C的Internationalization宏,即NSLocalizedString(key:tableName:bundle:value:comment:)方法,而且这个方法的所有参数都是默认值。调用方法:

<code class="hljs scss has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-function" style="box-sizing: border-box;">NSLocalizedString(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Hello"</span>,comment:<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"standard greeting"</span>)</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

7. 仍需注意持有循环

当两个对象互相强引用对方时,持有循环就会产生。而打断持有循环的方法,Swift和Objective-C是相同的。有三种引用类型:strong、week和unowned。 
那么该如何使用这三种引用类型?

  • strong 
    对于自己拥有的对象,使用此引用。默认情况下,对象的引用类型就是strong,并不需要显示地声明。
  • week 
    对于你不拥有其生命周期的对象,声明为week。week关键字只能修饰可空变量,因此用var和?来声明变量。
  • unowned 
    对于和引用者生命周期相同的对象,可以使用unowned。当一个引用对象总是有值,同事为了避免ARC将其设置为nil,我们可以使用unowned。 
    unowned其实等同于Ojective-C中的unsafe_unretained。 
    unowned引用必须是“不可空”的,因此它也不能被设置为nil。
0 0
原创粉丝点击