implicitly declaring library function 'objc_msgSend'with type '(id,SEL,...)' 警告


之前一直用objc_msgSend,但是沒注意apple的文檔提示,所以突然objc_msgSend crash了。

之前32位的時候沒問題,然后轉換為64位之后就會發生EXC_BAD_ACCESS問題。

當然apple再文檔([64-Bit Transition Guide for Cocoa Touch中有](https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/CocoaTouch64BitGuide/ConvertingYourAppto64-Bit/ConvertingYourAppto64-Bit.html#//apple_ref/doc/uid/TP40013501-CH3-SW26))中也有提到:

	Dispatch Objective-C Messages Using the Method Function’s Prototype
	An exception to the casting rule described above is when you are calling the objc_msgSend function or any other similar functions in the Objective-C runtime that send messages. Although the prototype for the message functions has a variadic form, the method function that is called by the Objective-C runtime does not share the same prototype. The Objective-C runtime directly dispatches to the function that implements the method, so the calling conventions are mismatched, as described previously. Therefore you must cast the objc_msgSend function to a prototype that matches the method function being called.

	Listing 2-14 shows the proper form for dispatching a message to an object using the low-level message functions. In this example, the doSomething: method takes a single parameter and does not have a variadic form. It casts the objc_msgSend function using the prototype of the method function. Note that a method function always takes an id variable and a selector as its first two parameters. After the objc_msgSend function is cast to a function pointer, the call is dispatched through that same function pointer

你必須先定義原型才可以使用,這樣才不會發生崩潰

	id Fun(int x,id y,...);
	id (*action)(int,id) = (id (*)(int,id)) Fun;
	action(1,@"s");

如果沒有返回值id改為void

原來我們這么寫:

	objc_msgSend(self,@selector(doSomething:), 0);
按照apple的規范:

	- (int) doSomething:(int) x { ... }
	- (void) doSomethingElse {
    	int (*action)(id, SEL, int) = (int (*)(id, SEL, int)) objc_msgSend;
    	action(self, @selector(doSomething:), 0);
	}

最終簡化之后64位調用:

	((void(*)(id, SEL,int))objc_msgSend)(self, @selector(doSomething:), 0);

原文:http://blog.iloss.me/post/kai-fa/2014-12-09-objc_msgsend?action=show_raw


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM