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).
分享到:
相关推荐
<body id='foo'> <!-- This space intentionally left blank. --> <section class="section-a pretty" id="bar1"> <h1>Hello World</h1><p>This is a test of the HTML5 parser.</p> <hr> & Nobody ...
<li><label>Foo: </label><input type="text" value="6.0" class="slideControl" maxlength="3" /></li> <li><label>Bar: </label><input type="text" value="4.0" class="slideControl" maxlength="3...
解开将XML转换为Python对象。 具有相似名称的兄弟姐妹被分组... 和:可以通过foobar.foo_bar访问_ <foobar><foo></foobar>可以通过foo_bar_baz和<foo><foo></foo>访问<foo> <foo><foo></foo>可以通过foo_bar.foo_baz访问
ReactNative 为程式化文本提供一个... var html = `<blockquote><p>Hello world <b>world</b> <i>foo</i> <blockquote>bar hahh</blockquote></p></blockquote>` return ( <View 标签:react
<br>function foo()<br>{<br> for(j = 0; j < 10; j++) <br> {<br> for(i = 0; i < 10; i++) <br> {<br> s = "string1";<br> k = Math.floor( Math.random()*10 );<br> }<br> for(i = 20; i > 9; i--) <br> {<br> s ...
foo2zjs的静态编译与驱动LJ1020的方法1
<users> <user id=’398′> <name>Foo</name> <email>foo@bar.com</name> </user> <user id=’867′> <name>Foobar</name> <email>foobar@foo.com</name> </user> </users>”; //load the xml string using ...
<EOF> <foo><bar>hello</bar></foo> <foo><bar>world</bar></foo> </data> EOF; $data = simplexml_load_string($string); print_r($data); print_r($data->foo); ?> 乍一看,结果很让人费解: 复制代码 代码如下...
<EOF> <foo><bar>hello</bar></foo> <foo><bar>world</bar></foo> </data> EOF; $data = simplexml_load_string($string); print_r($data); print_r($data->foo); ?> 乍一看,结果很让人费解: 复制代码 代码如下...
匹配: http://example.org/foo/index/index : http://example.org/foo/index/index config.xml :< jbh> < my> < type>static</ type> < route>foo.html</ route> < routers> < foo> < use>standard</ use> < args>...
复制代码代码如下: <div id=”wrap”> <div id=”main” class=”clearfix”> <div id=”content”> ccc </div> <div id=”side”> sss </div> </div> </div> <div id=”footer”> fff </div> 复制代码代码如下: ...
linux打印机驱动支持多种HP型号的打印机。 foo2zjs
<div id=foo> <div id=bar></div> <div id=baz> <div id=biz> </div> <span><span> </div> //jQuery code to count child elements $(#foo > div).size() 希望本文所述对大家的jQuery程序设计有所帮助。
< foo > 5 </ foo > < baz > 6/25/00 12:46 AM </ baz > < bar > This is the website value </ bar > < sub > < age > 1 </ age > < name > wangdei </ name > </ sub > </ MarkTest > 下面的程序是把xml转为成...
3 => FooFoo (divisible by 3, contains 3) 4 => 4 5 => BarBar (divisible by 5, contains 5) 6 => Foo (divisible by 3) 7 => QixQix (divisible by 7, contains 7) 8 => 8 9 => Foo 10 => Bar 13 => Foo 15 => Fo
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<Foo> foo = store.get(1234); ...
xmlbuilder2 用于的XML构建器。 安装: npm install xmlbuilder2 ... < foo> < bar>foobar</ bar> </ foo> < baz> </ root> 可以使用以下功能链创建: const { create } = require ( 'xmlbuilder2' ) ;
<div id=myDiv> <h1>简单路由</h1> <router to=/foo>Go to foo</router> <router to=/bar>Go to bar</router> // 渲染出口 <router></router> </div> 创建模板(组件): (也可以用import 引入外部组件) ...
<h2>Foo</h2> <div v-for =(item,> <!--<p>{{item }}</p>--> <h1 v-show=false>{{a[index] = item.bb}}</h1> <!-- <h1>index:{{index}}</h1>--> </div> <h2>a:{{a}}</h2> <h2>{{cc}}</h2> <...