nsobject

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

▼魔方 西西 提交于 2019-11-30 23:46:29
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? Go direct to the Objective-C runtime: #import <objc/runtime.h> /* originally posted version — works because eventually

self = [super init] revisited

早过忘川 提交于 2019-11-30 23:24:06
I stumbled upon this post In Objective-C why should I check if self = [super init] is not nil? I can understand this syntax : - (id)initWithString:(NSString *)aString { self = [super init]; if (self) { instanceString = [aString retain]; } return self; } or this syntax : - (id)init; { if (!(self = [super init])) return nil; // other stuff return self; } but I still don't understand the "standard" template syntax - init { if((self = [super init])) { // set up instance variables and whatever else here } return self; } Can someone tell as clearly as possible what (3) does more or less compared to

How Does AnyObject Conform to NSObjectProtocol?

我的未来我决定 提交于 2019-11-30 19:35:41
This question was inspired by mz2's answer on the question Check for object type fails with "is not a type" error . Consider an empty Swift class: class MyClass { } Attempting to call any NSObjectProtocol methods on an instance of this class will result in a compile-time error: let obj = MyClass() obj.isKindOfClass(MyClass.self) // Error: Value of type 'MyClass' has no member 'isKindOfClass' However, if I cast the instance as AnyObject , my object now conforms to NSObjectProtocol and I can call the instance methods defined by the protocol: let obj: AnyObject = MyClass() obj.isKindOfClass

What is the purpose of the -self method in NSObject-conformant classes?

余生长醉 提交于 2019-11-30 12:18:49
That's it. Why would anyone want (at least as a public API) a method such as that? Is there any practical use for it? The self method is useful for Key-Value Coding (KVC). With KVC, you can treat an object somewhat like a dictionary. You can access a property of the object using a string containing the name of the property, like this: [view valueForKey:@"superview"] . You walk down a chain of properties using a string containing a key path, like this: [view valueForKeyPath:@"superview.superview.center"] . Since NSObject has a self method, you can use self as the key or key path: [view

performSelector:withObject: and its retain behavior

三世轮回 提交于 2019-11-30 09:11:55
This is an already answer question within SO but I cannot find it in the Apple documentation anywhere . Could you point me in the right direction? Within the following topics Do I have to retain an object before passing it to -performSelector:withObject:afterDelay:? the effect on retain count of performSelector:withObject:afterDelay:inModes Is object that calls performSelector:withObject:afterDelay get retained by the NSRunLoop? the default behaviour seems to be the following: it retains the receiver and the argument(s) . I'm using the following code [[self delegate] performSelector:@selector

View outlet not available to File's Owner

半城伤御伤魂 提交于 2019-11-30 04:22:21
问题 I was getting the following message upon creating a new view controller. Everything was compiling a-okay in Xcode without errors, but the app was immediately quitting upon loading the new view from a RootViewController. Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "HomeView" nib but the view outlet was not set.' To resolve this, I tried connecting the File's Owner to the view, but the view

How Does AnyObject Conform to NSObjectProtocol?

自作多情 提交于 2019-11-30 03:48:59
问题 This question was inspired by mz2's answer on the question Check for object type fails with "is not a type" error. Consider an empty Swift class: class MyClass { } Attempting to call any NSObjectProtocol methods on an instance of this class will result in a compile-time error: let obj = MyClass() obj.isKindOfClass(MyClass.self) // Error: Value of type 'MyClass' has no member 'isKindOfClass' However, if I cast the instance as AnyObject , my object now conforms to NSObjectProtocol and I can

how can I make firebase database data the data source for UICollection View?

无人久伴 提交于 2019-11-29 21:44:03
问题 I want to change my model object datasource to firebase. I have a file that serves as the datasource for the UICollection view, homeViewController.swift . homeViewController.swift is a vertically arranged collectionViewCell and each cell has its own horizontally arranged collectionViewcell. This is the models.swift file import UIKit import Firebase class BusinessCategory: NSObject { var name: String? var featurebusiness: [SampleBusinesses]? var type: String? static func

What is the purpose of the -self method in NSObject-conformant classes?

别等时光非礼了梦想. 提交于 2019-11-29 17:53:16
问题 That's it. Why would anyone want (at least as a public API) a method such as that? Is there any practical use for it? 回答1: The self method is useful for Key-Value Coding (KVC). With KVC, you can treat an object somewhat like a dictionary. You can access a property of the object using a string containing the name of the property, like this: [view valueForKey:@"superview"] . You walk down a chain of properties using a string containing a key path, like this: [view valueForKeyPath:@"superview

performSelector:withObject: and its retain behavior

核能气质少年 提交于 2019-11-29 13:45:03
问题 This is an already answer question within SO but I cannot find it in the Apple documentation anywhere . Could you point me in the right direction? Within the following topics Do I have to retain an object before passing it to -performSelector:withObject:afterDelay:? the effect on retain count of performSelector:withObject:afterDelay:inModes Is object that calls performSelector:withObject:afterDelay get retained by the NSRunLoop? the default behaviour seems to be the following: it retains the