objective-c-runtime

Is NSObject class a part of the Objective-C runtime library today (instead of being a Foundation component)?

我的梦境 提交于 2019-12-22 05:58:31
问题 Looking at the Mac OS X 10.8's version of the Objective-C runtime library source code, I noticed that it's got a NSObject.mm file. As its name suggests, it's got the NSObject class implementation, as well as built-in autorelease pool and retain count implementations. However, versions of the ObjC runtime library prior to Mountain Lion's one didn't implement the NSObject class (they didn't have a NSObject.mm file, as you can see at the Mac OS X 10.7's Objective-C runtime library source code,

Find out if an Objective-C class overrides a method [duplicate]

可紊 提交于 2019-12-22 04:47:07
问题 This question already has answers here : Objective-C detect if class overrides inherited method (2 answers) Closed 4 years ago . How can I find out, at runtime, if a class overrides a method of its superclass? For example, I want to find out if a class has it's own implementation of isEqual: or hash , instead of relying on a super class. 回答1: You just need to get a list of the methods, and look for the one you want: #import <objc/runtime.h> BOOL hasMethod(Class cls, SEL sel) { unsigned int

Can Foundation tell me whether an Objective-C method requires a special structure return?

大城市里の小女人 提交于 2019-12-22 03:56:25
问题 Background as I understand it: Objective-C method invocations are basically a C function call with two hidden parameters (the receiver and the selector). The Objective-C runtime contains a function named objc_msgSend() that allows to invoke methods that way. Unfortunately, when a function returns a struct some special treatment may be needed. There are arcane (some might say insane) rules that govern whether the structure is returned like other values or whether it's actually returned by

Can Foundation tell me whether an Objective-C method requires a special structure return?

孤街醉人 提交于 2019-12-22 03:56:11
问题 Background as I understand it: Objective-C method invocations are basically a C function call with two hidden parameters (the receiver and the selector). The Objective-C runtime contains a function named objc_msgSend() that allows to invoke methods that way. Unfortunately, when a function returns a struct some special treatment may be needed. There are arcane (some might say insane) rules that govern whether the structure is returned like other values or whether it's actually returned by

Swift closure as AnyObject

怎甘沉沦 提交于 2019-12-22 03:43:41
问题 Im trying to use this method: class_addMethod() which in Obj-c is used like this: class_addMethod([self class], @selector(eventHandler), imp_implementationWithBlock(handler), "v@:"); And Im using it like this in Swift: class_addMethod(NSClassFromString("UIBarButtonItem"), "handler", imp_implementationWithBlock(handler), "v@:") It is an extension for UIBarButtonItem as you might have figured out. imp_implementationWithBlock takes a parameter of type AnyObject! How can I cast ()->() into

What's required to implement root class of Objective-C?

这一生的挚爱 提交于 2019-12-21 05:13:10
问题 I tried this code: // main.m #import <stdio.h> @interface Test + (void)test; @end @implementation Test + (void)test { printf("test"); } @end int main() { [Test test]; return 0; } with LLVM/Clang without any framework, it doesn't compiled with this error: Undefined symbols: "_objc_msgSend", referenced from: _main in main.o ld: symbol(s) not found clang: error: linker command failed with exit code 1 (use -v to see invocation) So I added libobjc.dylib . Code compiled, but threw this runtime

Objective-C runtime: What does declaring a variable of type Class (objc_class) conforming to a protocol mean?

僤鯓⒐⒋嵵緔 提交于 2019-12-21 01:20:08
问题 Class bah = [NSString class]; id object = [bah new]; Compiles with absolutely no issues. Class<NSSecureCoding> bah = [NSString class]; id object = [bah new]; Returns the error "No known class method for selector 'new'". Why does the first instance understand that it can call the +new method defined on NSObject but the second instance does not? 回答1: According to Apple's documentation: Protocols can’t be used to type class objects. Only instances can be statically typed to a protocol, just as

why does this code give EXC_BAD_ACCESS (using IMP)

不打扰是莪最后的温柔 提交于 2019-12-20 10:25:25
问题 This code gives me EXC_BAD_ACCESS, why? NSMutableDictionary *d = [[NSMutableDictionary alloc] init]; IMP imp= [d methodForSelector:@selector(setObject:forKey:) ]; imp(d, @selector( setObject:forKey:), @"obj", @"key"); I'm just starting using IMP, firs try.. no luck. Not sure why I get the error, also.. in the past, when I got EXC_BAD_ACCESS, the message was printed at the console, this time the error line is highlighted. Some notes: ARC is enabled, XCode 4.3.2, the project uses Objective-C++

Swizzling and super

落花浮王杯 提交于 2019-12-20 04:34:10
问题 I am trying to swizzle the canPerformAction:withSender: method for UIResponder and all its subclasses which have overridden this method. I am doing this by storing the original implementations in a dictionary keyed by class name; and looking up the dictionary in the swizzled version of the implementation before calling out to the original implementation. This seems to work fine for some cases, but fails when the original implementation calls out to super. Then my swizzled method continuously

Dynamically accessing local variables in Objective-C runtime

可紊 提交于 2019-12-20 03:14:13
问题 When attached to the debugger via Xcode, LLDB provides a useful view of local variables (the bottom left of the screenshot): I found an LLDB command frame variable (and gdb's info locals ) that provides a list of the local variables (as seen in the right side of the screenshot above). My hope is that this functionality is possible to perform on the device at runtime. For example, I can access the stack trace using backtrace_symbols() , the current selector via _cmd , and a few others. Has