1

I need to create a new instance of an object every time a method is called. So I need to create a loop that checks for the existence of a variable counter like newInstance1 and if it is found create newInstance2. How can you create a class from an nsstring? The goal is to create views that I can drag around on a grid that have represented objects in them. Here is that method that creates the new object and view

//method creates view with new fixtureIcon
- (IBAction)makeView:(id)sender {


    rectString = [NSString stringWithFormat:@"0,120,%f,%f", pm.fixWidth, pm.fixHeight];
    _try = [[fixtureIcon alloc]init];

    _try.fixImg = pm.fixImage;
    _try.name = pm.name;
    [fixController addObject:_try];     




    _testBox = [[addObjectView alloc]initWithFrame:NSRectFromString(rectString)];



    NSImageView *iconView = [[NSImageView alloc]initWithFrame:NSMakeRect(0, 0, 75, 75)];
    testView1Controller = [[NSViewController alloc]init];
    testView1Controller.view = _testBox;


    [_testBox setWantsLayer:YES];




    _testBox.layer.masksToBounds = YES;
    _testBox.layer.cornerRadius = 2.0;
    _testBox.layer.borderWidth = 2;
    _testBox.layer.borderColor = [[NSColor darkGrayColor]CGColor];

    //this tells the window to add our subview testbox to it's contentview

    [testView1Controller setView:_testBox];

    [testView1Controller bind:@"representedObject" toObject:fixController withKeyPath:@"content" options:nil];

    [mainWin addSubview:_testBox];
    NSTextField *obLabel = [[NSTextField alloc]initWithFrame:CGRectMake(0, 0, 50, 40)];
    [obLabel bind:@"value" toObject:testView1Controller withKeyPath:@"representedObject.type" options:nil];
    [obLabel setBackgroundColor:[NSColor clearColor]];
    [obLabel setSelectable:false];
    [obLabel setEditable:false];
    [obLabel setBackgroundColor:[NSColor grayColor]];
    [obLabel setBordered:false];
    [obLabel setTextColor:[NSColor whiteColor]];
    [iconView bind:@"value" toObject:testView1Controller withKeyPath:@"representedObject.fixImg" options:nil];
    //[_testBox addSubview:obLabel];
    [_testBox addSubview:iconView];

The reason I need new instances is so I don't overwrite the property values and lose my representedObject data for each view.

6
  • 1
    Consider using an NSMutableArray and doing addObject on it instead. Commented Feb 18, 2014 at 1:57
  • @Anna I am still going to have to change the initialized name of the instance that way.Right? Commented Feb 18, 2014 at 2:55
  • It might help if you explain in your question what you're trying to accomplish or what problem you're trying to solve by this. What I mean is: Declare an NSMutableArray named say myInstances. Create and add your objects to it. You can then access them by index (eg. [myInstances objectAtIndex:2]). Commented Feb 18, 2014 at 3:05
  • @Anna - I am creating a new instance of an object to be represented by a view controller. This is done dynamically. The user clicks a button that creates the new object. Commented Feb 18, 2014 at 3:11
  • @tyler_nichol. Please share some code, it might help us all understand what you're trying to do. From your comments and question is not clear at all. Commented Feb 18, 2014 at 4:03

1 Answer 1

4
id object = [[NSClassFromString(@"NameofClass") alloc] init];
Sign up to request clarification or add additional context in comments.

2 Comments

create instance of class not class name itself. Like someclass *stringforclassinstance = [someclass alloc]init];
This code creates an instance of a class of type NameofClass. Also, the subject line of your question reads: "create instance of class with NSString." This is what this code does. If you mean something different, please rephrase or edit your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.