nsobject

Need clarification on AnyObject in Swift

筅森魡賤 提交于 2019-12-06 06:19:58
Before I start, I have already read the Swift documentation. I am still trying to comprehend what AnyObject actually is. Is it a base class for all objects/classes in Swift, as NSObject is in Objective C? If I create an array of type [AnyObject] and populate it with Movie class instances, that would mean that AnyObject is a base class of the Movie class right? let someObjects: [AnyObject] = [ Movie(name: "2001: A Space Odyssey", director: "Stanley Kubrick"), Movie(name: "Moon", director: "Duncan Jones"), Movie(name: "Alien", director: "Ridley Scott") ] This should be true or else you wouldn't

How to create a Swift object in Objective-C?

萝らか妹 提交于 2019-12-06 02:44:01
If you define a swift class like this @objc class Cat { } In swift you can just do var c = Cat() But how do you make a Cat instance in Objective-C ? Subclassing NSObject works because you can then "alloc-init" but can we achieve this without subclassing an Objective-C class? The most direct way is to subclass Cat from NSObject . If you can't do that, you will need to make a class method or a function that returns a Cat . @objc class Cat { class func create() -> Cat { return Cat() } } func CreateCat() -> Cat { return Cat() } Cat *cat = [Cat create]; Cat *cat = CreateCat(); In modern objective-c

Inherit NSObject from C++ class

▼魔方 西西 提交于 2019-12-05 16:59:51
I'm emotionally attached to my C++ class, but I really want it to go into a tableview. Is there a way to subclass NSObject from a standard c++ class? this is what I tried to do: class Model : NSObject { public: NSString *key; }; The compiler did not like it one bit. It tells me the the base specifier must be a class. Is there any kind of sneaky thing I can do? Should I just make it a standard objective C++ object? You cannot mix inheritance between C++ and Objective-C classes. Instead, just make a standard Objective-C++ object which wraps your C++ object. Teach the Obj-C object how to pass

Using a typedef enum in my object Class

血红的双手。 提交于 2019-12-05 14:14:34
问题 I have a People class which holds various bits of into about a person. I would like to be able to identify what kind of person this is, so I thought I would try using a typedef enum for this since I have seen it done before and it seems like the cleanest solution. But, I am unsure how to declare this, then make it into a property. .h typedef enum { kPersonTypeFaculty, kPersonTypeStaff, kPersonTypeSearch } personType; @interface Person : NSObject { NSString *nameFirst; NSString *nameLast;

Overridden == function for Equatable type not called for custom class that subclasses NSCoding and NSObject [duplicate]

橙三吉。 提交于 2019-12-05 13:25:18
问题 This question already has answers here : NSObject subclass in Swift: hash vs hashValue, isEqual vs == (2 answers) Closed 3 years ago . The FooBar class below has to override the == function of the Equatable type. However, calling contains on an array of FooBar objects does not cause a breakpoint inside the custom == function to get invoked. Is it possible another == function is overriding this custom one? Note: Because FooBar must subclass from NSCoding and NSObject, FooBar does not list

How do I get class from NSString?

半腔热情 提交于 2019-12-05 10:36:26
for example, I have NSString *myClass = @"RootViewController" How do I get class from NSString? Try this id obj= [[NSClassFromString(@"RootViewController") alloc] init]; You can use Class class = NSClassFromString(@"RootViewController"); NSClassFromString Obtains a class by name. Class NSClassFromString ( NSString *aClassName ); Parameters aClassName The name of a class. Return Value The class object named by aClassName, or nil if no class by that name is currently loaded. If aClassName is nil, returns nil. Availability Available in iOS 2.0 and later. There is a lot of methods like this in

Accessing forwardInvocation'd methods with ARC?

隐身守侯 提交于 2019-12-05 04:23:06
问题 I'm writing a clone of OpenStruct in Objective-C, using forwardInvocation:. However, the compiler isn't aware of the forwarding at compile time apparently. Compiling with ARC gives me a ton of warnings. The code is open source and available on Github, but is currently compiled with -fno-objc-arc. If anyone could take a look at how I could make this ARC compatible, I'd greatly appreciate it. 回答1: I tried this code: OpenStruct *myStruct = [[OpenStruct alloc] initWithDictionary:myDictionary];

Do I need to call [super init] or [super initWithCoder], etc for NSObject

这一生的挚爱 提交于 2019-12-05 01:25:56
Typically when I subclass from a UI class I will call the superclass initializer of interest. However, I'm not sure of the implementation details of NSObject , and it seems like there's not much going on in terms of member vars, so I wonder: do I need to call [super init] if my subclass extends NSObject ? John Calsbeek Technically, no. The documentation for -[NSObject init] says that The init method defined in the NSObject class does no initialization; it simply returns self . Because it is documented and there's probably already a bunch of code that relies on it, that fact is highly unlikely

Objective-C forwardInvocation:

谁说胖子不能爱 提交于 2019-12-05 01:09:53
问题 I often do something like: CoolViewController *coolViewController = [[CoolViewController alloc] init]; [self.navigationController pushViewController:coolViewController animated:YES]; [coolViewController release]; How would I, in a category of UINavigationController , override forwardInvocation: so that I could just instead do: [self.navigationController pushCoolViewControllerAnimated:YES]; Please include the relevant code in your answer, not just an explanation. Thank you! Feel free to

Calling super.init() in initializer of NSObject subclass in Swift

谁说胖子不能爱 提交于 2019-12-04 23:28:35
I'm building an iOS app in Swift and drawing on the Lister sample project Apple provides. Lister uses two model objects: List and ListItem. I found that both of them do not call super.init() in their initializers even though they subclass NSObject. However, in the Objective-C version of Lister, both model objects (AAPLList and AAPLListItem) do call [super init] . The Swift Programming Language clearly states that “designated initializers must call a designated initializer from their immediate superclass.” (Rule 1 of Initializer Chaining in Initialization) What's going on here? Why is this an