nsobject

Objective-C: Why check nil before respondsToSelector:?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-21 06:47:37
问题 I've seen code like: if (delegate != nil && [delegate respondsToSelector:@selector(doSomething)]) ... But, sending a message to nil just returns nil (which evaluates to NO ), so why not just do: if ([delegate respondsToSelector:@selector(doSomething)]) ... Is the former faster if delegate == nil ? Either way, I prefer the latter cause it's less code. And, less is better than more . Every Unix pro knows that. 回答1: objc_msgSend, the function used to send dynamic messages in Objective-C

Why doesn't Swift force my designated initializer to call super?

ぐ巨炮叔叔 提交于 2019-12-19 10:13:43
问题 This code is legal in Swift: class Snapper : NSObject { var anim : UIDynamicAnimator init(referenceView:UIView) { self.anim = UIDynamicAnimator(referenceView:referenceView) // super.init() } } Observe that in my initializer I didn't call super.init() ; I commented out that line. But the Swift compiler doesn't complain. Why? I thought there was a rule that your designated initializer must call a designated initializer of its superclass. And I have a superclass, namely NSObject. Is this a bug?

Safe way to create singleton with init method in Objective-C

瘦欲@ 提交于 2019-12-19 05:23:11
问题 I would like to take the GCD approach of using shared instances to the next step so I created the following code: @implementation MyClass static id sharedInstance; #pragma mark Initialization + (instancetype)sharedInstance { static dispatch_once_t once; dispatch_once(&once, ^{ sharedInstance = [[self alloc] init]; }); return sharedInstance; } - (instancetype)init { if (sharedInstance) { return sharedInstance; } @synchronized(self) { self = [super init]; if (self) { sharedInstance = self; }

How to tell if a Class inherits from NSObject (Objective-C)

拟墨画扇 提交于 2019-12-19 04:07:41
问题 I'm working in Objective-C on the iPhone and need to know whether a 'Class' inherits from 'NSObject'. I tried checking if it responds to an NSObject selector: bool success = [myClass respondsToSelector:@selector(class)]; but you can guess what happened... it didn't even respond to "respondsToSelector:" so it throws a "does not implement doesNotRecognizeSelector:" exception. I tried to catch that exception, but it looks like it can't be caught with a @try-@catch. Any ideas? 回答1: Go direct to

Monotouch: convert an Object to NSObject

三世轮回 提交于 2019-12-18 05:38:13
问题 How is it possible to convert an Object instance to NSObject one? I've created a NSDictionary from NSDictionary.FromObjectAndKey(); This method wants an NSObject but I have custom object to pass in: int key = 2341; var val = new MyClass(); NSDictionary.FromObjectAndKey(val, key); // obviously it does not work!! How to fix this? Thank you in advance. 回答1: You can not convert an arbitrary object into an NSObject. The NSObject.FromObject will try to wrap common data types like numbers, strings,

Monotouch: convert an Object to NSObject

醉酒当歌 提交于 2019-12-18 05:38:04
问题 How is it possible to convert an Object instance to NSObject one? I've created a NSDictionary from NSDictionary.FromObjectAndKey(); This method wants an NSObject but I have custom object to pass in: int key = 2341; var val = new MyClass(); NSDictionary.FromObjectAndKey(val, key); // obviously it does not work!! How to fix this? Thank you in advance. 回答1: You can not convert an arbitrary object into an NSObject. The NSObject.FromObject will try to wrap common data types like numbers, strings,

Objective-C Is it safe to overwrite [NSObject initialize]?

爷,独闯天下 提交于 2019-12-18 04:23:12
问题 Basically, I have the following code (explained here: Objective-C Constants in Protocol) // MyProtocol.m const NSString *MYPROTOCOL_SIZE; const NSString *MYPROTOCOL_BOUNDS; @implementation NSObject(initializeConstantVariables) +(void) initialize { if (self == [NSObject class]) { NSString **str = (NSString **)&MYPROTOCOL_SIZE; *str = [[MyClass someStringLoadedFromAFile] stringByAppendingString:@"size"]; str = (NSString **)&MYPROTOCOL_BOUNDS; *str = [[MyClass someStringLoadedFromAFile]

Runtime error when using CoreFoundation objects in a swift NSObject subclass

跟風遠走 提交于 2019-12-17 20:46:56
问题 Here's a very simple class (subclass of NSObject ) that keeps a list of CGPath objects and appends one CGPath to the array on init : import Foundation class MyClass: NSObject { var list = [CGPath](); init() { list.append(CGPathCreateMutable()); } } When trying to use this class: var instance = MyClass(); println(instance.list.count); // runtime error after adding this line Yields an ugly crash: Playground execution failed: error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code

Why subclass NSObject?

喜你入骨 提交于 2019-12-17 19:07:25
问题 What is the purpose/use of NSObject in Objective-C? I see classes that extend NSObject like this: @interface Fraction : NSObject In C++ or Java, we don't use any variables like NSObject even though we have preprocessor directives and import statements in both Objective-C and Java. Why do classes explicitly inherit from NSObject in Objective-C? What are the consequences of not declaring inheritance from NSObject? 回答1: We use NSObject to explicitly state what a given class inherits from. I'm

How to instantiate an NSObject in the Interface builder .xib file

不羁的心 提交于 2019-12-14 03:58:10
问题 Greetings, I have a view based application project where I have created an NSObject class called "SquareClass". Now, from the Xcode's interface builder, I want to be able to instantiate that "SquareClass" into a square object with global scope, in such a way that, when I create actions from any UI controls(i.e textboxes, buttons etc...), I want to be able to call methods of that object within those actions. example: (void)MyAction1:(id)color { [square setColor:color]; } (void)MyAction2:(id