English 中文(简体)
2. 动态瞬时提交目标C
原标题:Dynamically instantiating object Objective-C

在贾瓦,我们可以像现在这样活跃地播下物体(只了解参数类型):

Class<?> msgClass = Class.forName(className);
constructor = msgClass.getConstructor(String.class, String.class, String.class);    
MyClass myClass = (MyClass) constructor.newInstance(myString, myString2, myString3);    

    public class MyClass {        
        public MyClass(String s, String s2, String s2){}
    }

是否在目标c中采取同样的做法,只知道向客观类别建筑商传递的参数类型。

提前感谢。

最佳回答

是的,取决于你重新做些什么。 Obj C non t有相同的语文级“施工器”,有<条码>init方法,这些方法通常与<条码><>>>即时使用法同时使用,但指出<条码>init是按框架公约而不是按光谱制定的,因此,操作时间并不“知道”你想要打什么“施工者”。

You can instantiate an object (the equivalent of allocating it) like this:

id myObj = class_createInstance(NSClassFromString(@"MyClass"));

虽然你也可以更直接地这样做:

id myObj = [NSClassFromString(@"MyClass") alloc];

但随后,你仍需要打上“in>。 你们想要这样做的方法。 如果你不想直接这样做,那么你需要知道该方法的挑选者,以便你能够发出正确的信息。 选择者是电文签名的静态表述。 您可以采用这种方法,通过这样的时间选择:

myObj = objc_msgSend(myObj, @selector(initWithStr1:str2:str3), myString1, myString2, myString3);
// Check myObj for nil which means a failed init.

ObjCprtime reference:

问题回答

暂无回答




相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签