Create dynamics names for UIViews [duplicate] - ios

This question already has answers here:
how to instantiate an object of class from string in Objective-C?
(2 answers)
Closed 9 years ago.
All is in my title, i want to create some dynamics names, to allocate some differents UIViews but randomly. For example, i want to make :
Level1 * level1view = [[Level1 alloc] init];
So i try to make some NSString like this :
ClasseMap = [NSString stringWithFormat:#"Level%i", Map];
NomMap = [NSString stringWithFormat:#"level%iview", NumeroMap];
id nouveau = NSClassFromString(ClasseMap);
nouveau * ViewTest;
ViewTest = [self valueForKey:NomMap];
ViewTest = [[nouveau alloc] init];
But it gives me : Use of undeclared identifier 'ViewTest'. How could i fix it please ? How could i get access to some variables (for example, level1view.example = YES;) ?
Thanks for your help ! =)

why dont you rather keep an array of views instead?
int Map = 0;
int NumeroMap = 1;
NSMutableArray *array = [[NSMutableArray alloc] init];
//add your views (levels?) to the array
[array addObject:someUIView];
[array addObject:someOtherUIView];
//now you can use your indexes to access the views you want
UIView *yourView = [array objectAtIndex:Map];
UIView *anotherView = [array objectAtIndex:NumeroMap];
i think what you are trying to do is not the way to go about things.

Related

Not using NSMutableArray correctly

Im trying to inset data in to the array (temp) but for some reason it saves the same data over an over. I all ready checked the _singleSeismicInfo to verify that it was handling different data(you can see it in the double "/" printData method). So i know the problem is with the MutableArray.
Im new to iOS so if theres something Im not doing right let me know.
NSMutableArray *temp = [[NSMutableArray alloc] init];
[_httpContent getTextFromHTTP];
for (int index = 0; index < 10; index++) {
NSString *line = _httpContent.lines[index];
[_singleSeismicInfo fillSeismicInfo:line];
//[_singleSeismicInfo printData];
[temp addObject:_singleSeismicInfo];
}
You're adding _singleSeismicInfo over and over without ever reassigning a new object to the variable as far as I can see. So it's the same object over and over because that's what you add.

How to load a JSON onto a NSArray

I need to pass to a Object a NSArray. In order it can show that array as Tags on the Interface, It works properly when using a manually added NSArray, But not i need to load a NSArray With a JSON Array Called Subjects I've done some code but it's not working out.
Also gives an error:
/Users/eddwinpaz/Documents/iOS-apps/mobile-app/mobile-app/UserDetailViewController.m:86:26: No visible #interface for 'NSArray' declares the selector 'insertObject:atIndex:'
This is the Code I'm Using
NSArray *subject_tag;
for (NSString* subject in [responseObject valueForKey:#"subjects"]) {
[subject_tag insertObject:subject];
}
CGRect frame = Scroller.frame;
frame.origin.y = 100;
frame.origin.x = 5;
frame.size.height = 150;
UIPillsView *pillsView = [[UIPillsView alloc] initWithFrame:frame];
[pillsView generatePillsFromStringsArray:subject_tag];
[Scroller addSubview:pillsView];
You have 3 problems here.
You never initialize your array, you only declare it. (This one is not actually causing the error and would just cause the code to fail silently once you fixed the next 2)
NSArrays are immutable. Elements cannot be added or removed after initialization. You need to use an NSMutableArray for this.
The method you are using does not exist in NSMutableArray anyway.
Here is what you should be doing:
NSMutableArray *subject_tag = [NSMutableArray new];
for (NSString* subject in [responseObject valueForKey:#"subjects"]) {
[subject_tag addObject:subject];
}

Store instances of same class in a NSMutableArray

I'm creating a little game using Sprite-kit and i'm having problems trying to store an object which means an enemy in the game.I use the same strategy that i use in my C++ programs but with objective-C and pointers the strategy doesn't work.
enemigo *nemesis = [[enemigo alloc] init];
self.almacenEnemigos = [[NSMutableArray alloc] initWithCapacity:kNumEnemigos];
for(int i = 0; i < kNumEnemigos; i++)
{
[nemesis pintarEnemigo:CGPointMake(20+5*i, 20+5*i)];
[self.almacenEnemigos addObject:nemesis];
}
I want to create four enemies but at the end i only have four times the same memory address according to a unique enemy. The var almacenEnemigos is an class attribute in the SKScene class.
You're only creating one instance of enemigo, there in your first line. You need to move the creation of the instances into the loop.
self.almacenEnemigos = [[NSMutableArray alloc] initWithCapacity:kNumEnemigos];
for(int i = 0; i < kNumEnemigos; i++)
{
enemigo *nemesis = [[enemigo alloc] init];
[nemesis pintarEnemigo:CGPointMake(20+5*i, 20+5*i)];
[self.almacenEnemigos addObject:nemesis];
}

Why does this array return 0 for an object count? [duplicate]

This question already has answers here:
NSMutableArray addObject not working
(2 answers)
Closed 9 years ago.
My array:
NSMutableArray *squareLocations;
CGPoint dotOne = CGPointMake(1, 1);
[squareLocations addObject:[NSValue valueWithCGPoint:dotOne]];
CGPoint dotTwo = CGPointMake(10, 10);
[squareLocations addObject:[NSValue valueWithCGPoint:dotTwo]];
CGPoint dotThree = CGPointMake(100, 100);
[squareLocations addObject:[NSValue valueWithCGPoint:dotThree]];
int num = [squareLocations count];
for (int i = 0; i < num; i++)
{
NSValue *pointLocation = [squareLocations objectAtIndex:i];
NSLog(#"%#", pointLocation);
}
When I ask squareLoctions for an object count, it returns zero? But just above asking for count I added three NSValue objects??? Anyone have any ideas?
You need to initialise the array first
NSMutableArray *squareLocations = [[NSMutableArray alloc] init];
That array is returning 0 because you are actually not asking an array for its size.
The object you assume to be an array has neither been allocated, nor being correctly initialised.
You are asking an instance that is currently initialised as being nil. The fun part is that it does not crash as Objective-C will allow you to call any method (selector) on a nil instance (well, that term is not quiet correct). Just, those nil instances will always return 0, NO, 0.0f, 0.0 or nil depending on the expected type when being asked for their return values. In other words, it always returns a value that when being casted towards the expected type, will be set to zero.
To fix that issue you will need to allocate, initialise and assign an instance of an NSMutableArray to your variable.
That could either been done using the proper alloc and init method combination:
NSMutableArray *squareLocations = [[NSMutableArray alloc] init];
Or you could use one of the convenience constructors like for example:
NSMutableArray *squareLocations = [NSMutableArray array];
Next time you encounter such weird behaviour, check if the instance in question is not nil first.

Binding a number to NSTextField with an NSObjectController, but cannot display

I am pretty new for cocoa programming.
I am learning binding, trying to make a simple binding code as:
- (void)awakeFromNib
{
self.aValue = [[Model alloc] init];
NSString *aKey = #"value";
NSDictionary *aDic = [[NSDictionary alloc] initWithObjectsAndKeys:self.aValue, aKey, nil];
self.anObjctCtrler = [[NSObjectController alloc] initWithContent:nil];
[self.anObjctCtrler setContent:aDic];
NSLog(#"%ld", [[[[self.anObjctCtrler content] valueForKey:#"value"] number] integerValue]);
}
The anObjectCtrler is an NSObjectController in the Interface Builder, and I "bind" a NSTextField to Object Controller, the class of anObjectCtrler, at a model key path of "value" and controller key of "selection"; the Obeject Controller has referencing outlet to File's Owner as anObeject.
When I run the codes, the NSTextField displays "No Selection", not as expected as the value of "self.aValue". You can see I check the content of the object controller by using an output of "NSLog...", it shows the correct value.
Anyone can help me to solve this, please?
Many Thanks.

Resources