`
zani
  • 浏览: 349471 次
  • 性别: Icon_minigender_1
  • 来自: 福州
社区版块
存档分类
最新评论

id foo1, NSObject *foo2和id<NSObject> foo3区别

 
阅读更多

 

id foo1;
NSObject *foo2;
id<NSObject> foo3;

 

 

  • The first one is the most common.It simply declares a pointer to some Objective-C object (see /usr/include/objc/objc.h). id gives the compiler no information about the actual type of the object, so the compiler cannot do compile-time type checking for you.


 

  •  

Just because we know that an id is an Objective-C object does not mean that it points to an object that derives from NSObject, or that it even has common methods like retain and release.

One solution is to statically type our variable using NSObject* as shown in number 2 above.

This gives the compiler information about the class of the object pointed to by foo2 so the compiler can warn if you send a message to foo2 that an NSObject doesn't respond to. This means you can safely call retain, release, description, etc., but the compiler will warn if you call length or count or anything that an NSObject doesn't respond to.



 

  • Declaring an object as id<NSObject> tells the compiler that you don't care what type the object is, but you do care that it conforms to the specified NSObject protocol**.

    ** the protocol (@protocol) named NSObject. There is also a class named NSObject that does indeed conform to the NSObject protocol, but they are two different thing

    The compiler will ensure that all objects you assign to that pointer conform to the required protocol.

    A pointer typed like this can safely hold any NSObject (because NSObject conforms to theNSObject protocol), but it could also hold any NSProxy, because NSProxy also conforms to the NSObject protocol.

    In english, the declaration id<NSObject> foo3; says "foo3 is a pointer to an object of any type that behaves like an NSObject".

    This is very powerful, convenient, and expressive. In reality, we often don't care what type an object is, we just care that it responds to the messages that we want to send it (e.g., retain, release).

分享到:
评论

相关推荐

    [示例][PHP]HTML5解析和序列化的PHP库.zip

    &lt;body id='foo'&gt; &lt;!-- This space intentionally left blank. --&gt; &lt;section class="section-a pretty" id="bar1"&gt; &lt;h1&gt;Hello World&lt;/h1&gt;&lt;p&gt;This is a test of the HTML5 parser.&lt;/p&gt; &lt;hr&gt; &amp; Nobody ...

    jQuery CSS3猎豹橙色大巴动画背景.zip

    &lt;li&gt;&lt;label&gt;Foo: &lt;/label&gt;&lt;input type="text" value="6.0" class="slideControl" maxlength="3" /&gt;&lt;/li&gt; &lt;li&gt;&lt;label&gt;Bar: &lt;/label&gt;&lt;input type="text" value="4.0" class="slideControl" maxlength="3...

    解开:将XML转换为Python对象

    解开将XML转换为Python对象。 具有相似名称的兄弟姐妹被分组... 和:可以通过foobar.foo_bar访问_ &lt;foobar&gt;&lt;foo&gt;&lt;/foobar&gt;可以通过foo_bar_baz和&lt;foo&gt;&lt;foo&gt;&lt;/foo&gt;访问&lt;foo&gt; &lt;foo&gt;&lt;foo&gt;&lt;/foo&gt;可以通过foo_bar.foo_baz访问

    文本解析控件react-native-htmltext.zip

    ReactNative 为程式化文本提供一个... var html = `&lt;blockquote&gt;&lt;p&gt;Hello world &lt;b&gt;world&lt;/b&gt; &lt;i&gt;foo&lt;/i&gt; &lt;blockquote&gt;bar hahh&lt;/blockquote&gt;&lt;/p&gt;&lt;/blockquote&gt;`  return (  &lt;View  标签:react

    javascript-code-improver

    &lt;br&gt;function foo()&lt;br&gt;{&lt;br&gt; for(j = 0; j &lt; 10; j++) &lt;br&gt; {&lt;br&gt; for(i = 0; i &lt; 10; i++) &lt;br&gt; {&lt;br&gt; s = "string1";&lt;br&gt; k = Math.floor( Math.random()*10 );&lt;br&gt; }&lt;br&gt; for(i = 20; i &gt; 9; i--) &lt;br&gt; {&lt;br&gt; s ...

    foo2zjs的静态编译与驱动LJ1020的方法1

    foo2zjs的静态编译与驱动LJ1020的方法1

    PHP XML数据解析代码

    &lt;users&gt; &lt;user id=’398′&gt; &lt;name&gt;Foo&lt;/name&gt; &lt;email&gt;foo@bar.com&lt;/name&gt; &lt;/user&gt; &lt;user id=’867′&gt; &lt;name&gt;Foobar&lt;/name&gt; &lt;email&gt;foobar@foo.com&lt;/name&gt; &lt;/user&gt; &lt;/users&gt;”; //load the xml string using ...

    PHP中simplexml_load_string函数使用说明

    &lt;EOF&gt; &lt;foo&gt;&lt;bar&gt;hello&lt;/bar&gt;&lt;/foo&gt; &lt;foo&gt;&lt;bar&gt;world&lt;/bar&gt;&lt;/foo&gt; &lt;/data&gt; EOF; $data = simplexml_load_string($string); print_r($data); print_r($data-&gt;foo); ?&gt; 乍一看,结果很让人费解: 复制代码 代码如下...

    php中simplexml_load_string使用实例分享

    &lt;EOF&gt; &lt;foo&gt;&lt;bar&gt;hello&lt;/bar&gt;&lt;/foo&gt; &lt;foo&gt;&lt;bar&gt;world&lt;/bar&gt;&lt;/foo&gt; &lt;/data&gt; EOF; $data = simplexml_load_string($string); print_r($data); print_r($data-&gt;foo); ?&gt; 乍一看,结果很让人费解: 复制代码 代码如下...

    jbh_router:用于 Magento 的基于 Zend 框架的路由器

    匹配: http://example.org/foo/index/index : http://example.org/foo/index/index config.xml :&lt; jbh&gt; &lt; my&gt; &lt; type&gt;static&lt;/ type&gt; &lt; route&gt;foo.html&lt;/ route&gt; &lt; routers&gt; &lt; foo&gt; &lt; use&gt;standard&lt;/ use&gt; &lt; args&gt;...

    footer 贴在底部的布局实现代码

    复制代码代码如下: &lt;div id=”wrap”&gt; &lt;div id=”main” class=”clearfix”&gt; &lt;div id=”content”&gt; ccc &lt;/div&gt; &lt;div id=”side”&gt; sss &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;div id=”footer”&gt; fff &lt;/div&gt; 复制代码代码如下: ...

    linux打印机驱动foo2zjs

    linux打印机驱动支持多种HP型号的打印机。 foo2zjs

    jQuery统计指定子元素数量的方法

    &lt;div id=foo&gt; &lt;div id=bar&gt;&lt;/div&gt; &lt;div id=baz&gt; &lt;div id=biz&gt; &lt;/div&gt; &lt;span&gt;&lt;span&gt; &lt;/div&gt; //jQuery code to count child elements $(#foo &gt; div).size() 希望本文所述对大家的jQuery程序设计有所帮助。

    jox-1.17b3.jar

    &lt; foo &gt; 5 &lt;/ foo &gt; &lt; baz &gt; 6/25/00 12:46 AM &lt;/ baz &gt; &lt; bar &gt; This is the website value &lt;/ bar &gt; &lt; sub &gt; &lt; age &gt; 1 &lt;/ age &gt; &lt; name &gt; wangdei &lt;/ name &gt; &lt;/ sub &gt; &lt;/ MarkTest &gt; 下面的程序是把xml转为成...

    mihoubahmed.github.io:我的FooBarQix Kata解决方案

    3 =&gt; FooFoo (divisible by 3, contains 3) 4 =&gt; 4 5 =&gt; BarBar (divisible by 5, contains 5) 6 =&gt; Foo (divisible by 3) 7 =&gt; QixQix (divisible by 7, contains 7) 8 =&gt; 8 9 =&gt; Foo 10 =&gt; Bar 13 =&gt; Foo 15 =&gt; Fo

    Java对象存储java-dirty.zip

    store.put(new Foo(1,2)); store.all().forEach(System.out::println); store.reverse().forEach(System.out::println); store.from(100).forEach(System.out::println); Optional&lt;Foo&gt; foo = store.get(1234); ...

    xmlbuilder2:用于node.js的XML构建器

    xmlbuilder2 用于的XML构建器。 安装: npm install xmlbuilder2 ... &lt; foo&gt; &lt; bar&gt;foobar&lt;/ bar&gt; &lt;/ foo&gt; &lt; baz&gt; &lt;/ root&gt; 可以使用以下功能链创建: const { create } = require ( 'xmlbuilder2' ) ;

    vue-router 路由基础的详解

    &lt;div id=myDiv&gt; &lt;h1&gt;简单路由&lt;/h1&gt; &lt;router to=/foo&gt;Go to foo&lt;/router&gt; &lt;router to=/bar&gt;Go to bar&lt;/router&gt; // 渲染出口 &lt;router&gt;&lt;/router&gt; &lt;/div&gt; 创建模板(组件): (也可以用import 引入外部组件) ...

    vue2.0 computed 计算list循环后累加值的实例

    &lt;h2&gt;Foo&lt;/h2&gt; &lt;div v-for =(item,&gt; &lt;!--&lt;p&gt;{{item }}&lt;/p&gt;--&gt; &lt;h1 v-show=false&gt;{{a[index] = item.bb}}&lt;/h1&gt; &lt;!-- &lt;h1&gt;index:{{index}}&lt;/h1&gt;--&gt; &lt;/div&gt; &lt;h2&gt;a:{{a}}&lt;/h2&gt; &lt;h2&gt;{{cc}}&lt;/h2&gt; &lt;...

Global site tag (gtag.js) - Google Analytics