我正在创建一个类,给定一个object目标,一个selector要监视,并且displayTitle将以这种格式输出一个字符串:@"displayTitle: object.selector".然后,它通过KVO寄存器本身,以便任何时候value的object.selector变化,它可以通知一个视图控制器更新视图.我使用它作为一种抽象和可重用的方式向用户显示对象的各种属性的描述.
当我尝试获取值时object.selector,我无法做到,[object performSelector:selector]因为当您使用带动态选择器的performSelector时,LLVM会出错.所以,我完全按照这个答案提出的建议:我用了objc_msgSend(object, selector).
- (instancetype)initWithSelector:(SEL)selector onObject:(NSObject*)object displayTitle:(NSString*)displayTitle {
self = [super init];
if (self) {
id value;
if ([object respondsToSelector:selector) {
// Used objc_msgSend instead of performSelector to suppress a LLVM warning which was caused by using a dynamic selector.
value = objc_msgSend(object, selector);
} else {
return nil;
}
[self setItemDescription:[NSString stringWithFormat:@"%@: %@", displayTitle, value]]; …Run Code Online (Sandbox Code Playgroud) 我的应用程序在应用程序商店,我已经启用了我的应用程序内的批评.报告崩溃后:
Threads
_________________________________
Thread: Unknown Name (Crashed)
0 libobjc.A.dylib 0x00000001973f7bdc objc_msgSend + 28
1 UIKit 0x000000018b59952c -[UIKeyboardImpl centerFilled] + 88
2 UIKit 0x000000018b90eb00 -[UIKBBackgroundView refreshStyleForKeyplane:inputTraits:] + 300
3 UIKit 0x000000018b599284 -[UIKeyboardLayoutStar updateBackgroundIfNeeded] + 384
4 UIKit 0x000000018b591cc4 -[UIKeyboardLayoutStar setKeyplaneName:] + 2672
5 UIKit 0x000000018b4d2f7c -[UIKeyboardLayoutStar setShift:] + 180
6 UIKit 0x000000018b4d2ca8 -[UIKeyboardImpl notifyShiftState] + 84
7 CoreFoundation 0x0000000186c92a50 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 28
8 CoreFoundation 0x0000000186c8f9dc __CFRunLoopDoObservers + 356
9 CoreFoundation 0x0000000186c8fd24 __CFRunLoopRun + 680
10 CoreFoundation 0x0000000186bbd0a4 CFRunLoopRunSpecific + 392
11 …Run Code Online (Sandbox Code Playgroud) segmentation-fault armv7 objc-message-send crittercism ios8.1
我试图理解Smalltalk中更好的反映.我使用的是最新版本的Squeak(v4.3).我想拦截发送到我的一个类的实例的每个消息.我假设我可以覆盖这个方法,ProtoObject>>withArgs:executeMethod但是StéphaneDucasse向我解释说,出于性能原因,这个方法没有被使用(这是我自己对他答案的总结).我应该覆盖哪种方法/如何拦截已发送的消息?
这是我尝试的代码:
Object subclass: #C
instanceVariableNames: 'i'
classVariableNames: ''
poolDictionaries: ''
category: 'CSE3009'.
C class compile: 'newWithi: anInt
^(self new) i: anInt ; yourself.'.
C compile: 'withArgs: someArgs executeMethod: aMethod
Transcript show: ''Caught: ''.
^ super withArgs: someArgs executeMethod aMethod.'.
C compile: 'foo: aText
Transcript show: aText.
Transcript show: i.
Transcript cr.'.
C compile: 'i: anInt
i := anInt.'.
o := C newWithi: 42.
o foo: 'This is foo: '.
Run Code Online (Sandbox Code Playgroud)
执行这整段代码会产生:
This is foo: 42
Run Code Online (Sandbox Code Playgroud)
当我想要:
Caught: This …Run Code Online (Sandbox Code Playgroud) 我在库中有一个看起来像这样的方法:
- (id)initWithSomeObjects:(NSString *)something, ... NS_REQUIRES_NIL_TERMINATION;
Run Code Online (Sandbox Code Playgroud)
我真的想用数组而不是var args来调用它,因为我想传入的对象数量是可以改变的.
有没有办法,使用performSelector或NSInvocation或objc_msgSend或其他什么,我可以调用var args方法,参数来自数组?
我在friday.com上阅读这篇很棒的文章.bbum显示了一些Objective-c代码和相应的程序集.如何查看objective-c汇编代码?
假设我正在使用OS X终端进行编译gcc.
我有以下崩溃报告.它只发生在iOS 8上.我确切知道如何重现它,但不能在我的生活中找出问题在我的代码中的位置.当我使用断点并尝试单步执行应用程序时,崩溃不会发生.如何在我的应用中找到问题所在?
Exception Type: SIGSEGV
Exception Codes: SEGV_ACCERR at 0x10
Crashed Thread: 0
Application Specific Information:
objc_msgSend() selector name: respondsToSelector:
Thread 0 Crashed:
0 libobjc.A.dylib 0x00000001974b3bd0 objc_msgSend + 16
1 UIKit 0x000000018ab960f4 -[UIToolbar _didMoveFromWindow:toWindow:] + 108
2 UIKit 0x000000018aa30d44 -[UIView(Internal) _didMoveFromWindow:toWindow:] + 700
3 UIKit 0x000000018aa30d44 -[UIView(Internal) _didMoveFromWindow:toWindow:] + 700
4 UIKit 0x000000018aa30d44 -[UIView(Internal) _didMoveFromWindow:toWindow:] + 700
5 UIKit 0x000000018aa303dc __45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 140
6 UIKit 0x000000018aa302bc -[UIView(Hierarchy) _postMovedFromSuperview:] + 480
7 UIKit 0x000000018aa3c0b0 -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1788
8 UIKit …Run Code Online (Sandbox Code Playgroud) objective-c ×3
armv7 ×1
assembly ×1
compilation ×1
crittercism ×1
gcc ×1
ios8 ×1
ios8.1 ×1
metaclass ×1
nsinvocation ×1
reflection ×1
selector ×1
smalltalk ×1
squeak ×1