- 浏览: 583162 次
- 性别:
- 来自: 广州
文章分类
最新评论
-
JYY282:
[i][/i]
Ecshop与Shopex的比较 -
qq247890212:
我也遇见这问题了,真诡异。重新下载个猫换了就好了。 太浪费时间 ...
诡异:ClassNotFoundException: org.springframework.web.filter.CharacterEncoding
From: http://hi.baidu.com/colorapple/blog/item/bc40470a87763d34b0351d1f.html
There's often confusion about the difference between the following three declarations in Objective-C:
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. Thus, the
compiler will let you send any (*) message to objects declared id
. Actually, this is why the common idiom of [[Foo alloc] init]
doesn't cause the compiler to complain. +alloc
is declared to return type id
, so the compiler won't yell when you then send the returned object the message init
(or even initWithMyFoo:blah
).
So, objects declared using id
are
just dynamically typed at runtime. The compiler has no useful
information about the object's real type, so it can't warn you if you
send it a message that it may not respond to.
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.
So, declaring a generic pointer of type NSObject* is very similar to
what you would do in other languages, like Java, but it's really a bit
too restrictive for a language as flexible as Objective-C. Despite what
you may have learned at one point, not all Foundation/Cocoa objects
derive from NSObject. As an example, NSProxy is not derived from
NSObject, so the foo2
pointer above
would not be able to hold an NSProxy subclass, even though NSProxy does
implement common methods like retain and release. What you really want
is a pointer to any object that behaves like an NSObject. And that's exactly what the third case does.
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
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 the NSObject 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).
So how do you decide which form you want to use? It's pretty easy. If
you don't want (or can't have) any type checking, then use a plain id
. This is very common for return types on methods that don't know the type of object they're returning (e.g., +alloc
). It is also common to declare delegates to be type id
, because delegates are generally checked at runtime with respondsToSelector:
, and they usually aren't retained.
However, if you do want compile-time type checking, you must decide
between the second and third cases. Well, let me just help you out—you
want the third case! :-) I've very, very, VERY rarely seen a situation
where NSObject *
worked but id<NSObject>
would not. And using the protocol form has the advantage that it will
work with NSProxys. You may think that you never use NSProxys, but
Cocoa's distributed objects system makes heavy use of NSProxy
subclasses. Additionally, the common case is that you simply want to
ensure that an object can be retained or released, and in that case the
protocol form conveys that intent better; you really don't care what
class the object is, you only care that it behaves like an NSObject.
发表评论
-
Objective-C 与 C++ 的异同
2013-04-02 12:03 1403http://www.cnblogs.com/y041039 ... -
Cocos2D-X是全球知名的开源跨平台手机游戏引擎
2013-01-22 10:05 2762http://www.oschina.net/p/cocos ... -
iOS Keyboard 键盘高度变化 自适应
2013-01-15 15:43 3257[[NSNotificationCenter default ... -
iOS使用自定义字体
2012-11-27 12:11 12154From: http://blog.csdn.net/csy1 ... -
4 款类似 Facebook/Path 切换效果的 iOS 组件
2012-11-27 12:03 2205From: http://blog.csdn.net/lia ... -
Path 2.0的UI界面设计详细介绍
2012-11-27 11:56 1476如Path的创始人Dave Morin ... -
史上最全的App Store邮箱列表
2012-11-27 11:51 1277From: http://roybaby.blog.51cto ... -
iOS从info.plist 获取项目的名称及版本号
2012-11-16 10:54 1681From: http://blog.sina.com.cn/s ... -
MapKit annotation drag and drop with callout info update
2012-10-13 10:38 2414http://hollowout.blogspot ... -
NSArray 或NSDictionary 调用writeToFile方法失败原因
2012-08-31 10:03 4499NSArray 或NSDictionary 调用writeTo ... -
如何让IOS应用从容地崩溃
2012-08-30 15:25 1626From: http://www.cocoachina.com ... -
iOS中判断设备系统版本
2012-08-29 17:17 31721在iOS开发中,经常要考虑系统的向下兼容,如果使用 ... -
iOS 汉字转拼音
2012-08-21 16:42 1476From: http://www.cnblogs.com/v2 ... -
iOS模拟器截图工具
2012-08-17 16:35 1665From: http://magicalboy.com/ios ... -
XCode下的iOS单元测试
2012-08-10 17:47 1174From: http://mobile.51cto.com/ ... -
AFNetworking
2012-08-08 10:54 4657AFNetworking on github: https:/ ... -
Wrapping Conventions
2012-08-01 15:54 841Wrapping Conventions ... -
Core Animation如何使显式动画结束时的值直接作用Layer
2012-08-01 14:51 3801(1)使用隐式动画会直接改变layer的属性值,如: ima ... -
How To Debug Memory Leaks with XCode and Instruments Tutoria
2012-07-31 16:30 1065From: http://www.raywenderlich. ... -
Using Properties in Objective-C Tutorial
2012-07-31 16:27 938From: http://www.raywenderlich. ...
相关推荐
<key>id</key> <integer>158</integer> <key>name</key> <string>应用1</string> <key>version</key> <integer>0</integer> <!-- 其他相关信息 --> </dict> <!-- 更多的字典条目 --> </array> ``` 开发...
<book id="bk101"> <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description>An...
@interface ConcreteProductA : NSObject <ProductProtocol> @end @implementation ConcreteProductA -(void)execute { NSLog(@"ConcreteProductA is executing."); } @end @interface ConcreteProductB : ...
<book id="1"> <title>书名1</title> <author>作者1</author> </book> <book id="2"> <title>书名2</title> <author>作者2</author> </book> </books> ``` 我们可以使用TBXML来打印XML的内容: ```objc ...
- (void)colleague:(id<Colleague>)colleague performAction:(NSString *)action; @end // ConcreteMediator类 @interface ConcreteMediator : NSObject <Mediator> @property (nonatomic, strong) NSMutableArray ...
@interface YourXMLParser : NSObject <NSXMLParserDelegate> @property (nonatomic, strong) NSMutableArray<Item *> *items; @end @implementation YourXMLParser - (void)parser:(NSXMLParser *)parser ...
@protocol ZSegmentedControlDelegate <NSObject> - (void)setSelectedIndex:(NSUInteger)selectedIndex; @end typedef NS_ENUM(NSInteger, MSegmentedControlIndicatorStyle){ ...
#import <Foundation/Foundation.h> ...@property(nonatomic,assign) id<RMIAPHelperDelegate> delegate; -(void)setup; -(void)destroy; -(void)buy:(NSString*)productId; -(void)restore; @end
#import <Foundation/Foundation.h> @interface MyObject : NSObject @property (nonatomic, strong) NSString *name; @end // MyObject.m #import "MyObject.h" @implementation MyObject - (instancetype)...
@protocol CalculatorProtocol <NSObject> @required - (NSNumber *)add:(NSNumber *)num1 with:(NSNumber *)num2; - (NSNumber *)subtract:(NSNumber *)num1 from:(NSNumber *)num2; @end ``` 接下来,我们可以...
`<NSObject>`表示这个协议继承自内置的`NSObject`协议,这样我们可以使用`NSObject`的方法,如`isEqual:`和`description`。 然后,创建一个类(如`EventProcessor`),它将使用委托来处理事件。在`EventProcessor`...
@property (nonatomic, strong) NSMutableArray<id<ObserverProtocol>> *observers; - (void)attachObserver:(id<ObserverProtocol>)observer; - (void)detachObserver:(id<ObserverProtocol>)observer; - (void)...
iOS NSObject对象的本质、内存分配、ISA指针及superclass底层源码分析 iOS NSObject对象是iOS开发中最基本也是最重要的对象之一,我们在使用它时往往只是停留在会用的阶段,却没有深入了解过它的本质。今天我们就来...
- (id<NotificationObserver>)createObserverForNotificationType:(NSString *)notificationType; @end ``` #### 4.2 创建具体工厂 接着,创建具体工厂类实现这个协议,例如`DefaultNotificationObserverFactory`:...
楼主项目中需要有一个轮播图,因为比较简单,就自己写了个,因为是从网上弄得图片 所以用了SDWebImage 这个三方库 当然自己也可以去掉 类型后面有*号 如用使用 请自行...@property (nonatomic,weak)id<TJXViewDelegate>
@"user_id": mts_key(userId), @"creation_date": mts_key(creationDate), @"website": mts_key(website), @"user_stats.views": mts_key(views), // <-- KeyPath access @"user_stats.ranking":...
@interface ObjectAdapter : NSObject<Target> @property (nonatomic, weak) id<Adaptee> adaptee; @end @implementation ObjectAdapter - (void)Request { [self.adaptee SpecificRequest]; } @end ``` 在这个...
@property (nonatomic, strong) id<AlgorithmProtocol> algorithm; - (void)performAction; @end @implementation Context - (void)performAction { [self.algorithm execute]; } @end ``` 4. **使用策略**:在...
@property (nonatomic, strong) NSMutableArray<id<CompositeProtocol>> *children; - (void)performOperation; @end ``` 在Objective-C中,组合模式的实现通常涉及以下几个步骤: 1. 定义Component协议或基类。 2...
@property (nonatomic, weak) id<PassValueDelegate> delegate; - (void)sendValue:(NSString *)value; @end ``` 在`ValueSender`类中实现`sendValue:`方法,通过代理将值传递出去: ```objc @implementation ...