Syntax help - Variable as object name [duplicate] - iphone

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
create multiple variables based on an int count
Objective C Equivalent of PHP's “Variable Variables”
How would I create and reference an object using a variable as it's name?
Example -
for (int i=1; i<7; i++) {
CGRect ("myRectNum & i") = myImageView.bounds;
}
("myRectNum & 5").height etc ..

There isn't anything like this in the Objective-C language, and in general it's not going to be a very practical way of referring to data (what if you typo a string? the compiler won't be able to catch it). I won't get into second-guessing what you actually want to do (that would depend on the goal of this part of your application), but you can use an NSMutableDictionary to get a similar effect:
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
for (int i = 0; i < 7; i++)
{
NSString *key = [NSString stringWithFormat:#"myRectNum & %d", i];
NSValue *value = [NSValue valueWithCGRect:myImageView.bounds];
[dict setObject:value forKey:key];
}
Then to fetch the values back out again:
NSValue *value = [dict objectForKey:#"myRectNum & 5"];
CGRect bounds = [value CGRectValue];
NSLog(#"height = %f", bounds.size.height);

Related

Create 1 million NSMutableArrays with unique names easily

After teaching myself (mostly from this website) I have finally been unable to find a solution to a problem.
I am trying to easily create 1 million NSMutableArrays with unique names. What I mean by easily is not having 'hard code' all of the individual arrays.
What I would like to do is something like this:
for (int i = 1; i <= 1000000; i++) {
NSString *arrayNumber = [NSString stringWithFormat:#"%d", i];
NSString *millionArrayNumber = #"millionArrayNumber";
NSString *arrayName = [millionArrayNumber stringByAppendingString:arrayNumber];
NSMutableArray *[NSString arrayName] = [NSMutableArray array];
}
I can understand why this doesn't work but I can't think of another way of doing it. I thought it might be possible to use something similar to:
[button setTag = 1];
If I change the code to:
for (int i = 1; i <= 1000000; i++) {
NSMutableArray *millionArray = [NSMutableArray array];
[millionArray setTag: i];
}
I would then use the tag to control the arrays as you can do with buttons. This doesn't work for arrays though.
I hope I haven't been ambiguous.
I'm not sure how to convert a string into literal code like you're doing, but I think it's possible.
However, you can always just kick 1 million arrays to an NSMutableDictionary and give each one your unique key name.
NSMutableDictionary *arrayStore = [NSMutableDictionary dictionary];
for (int i = 1; i <= 1000000; i++) {
NSString *arrayNumber = [NSString stringWithFormat:#"%d", i];
NSString *millionArrayNumber = #"millionArrayNumber";
NSString *arrayName = [millionArrayNumber stringByAppendingString:arrayNumber];
arrayStore[arrayName] = [NSMutableArray array];
}
And obviously to get whatever array you want later, you just use:
NSMutableArray *uniqueArray = arrayStore[arrayName];
Edit: Not sure if you want to keep the number as the unique key as mentioned in the comment below but if so, you can do it with this:
NSMutableDictionary *arrayStore = [NSMutableDictionary dictionary];
for (int i = 1; i <= 1000000; i++) {
arrayStore[#(i)] = [NSMutableArray array];
}
And access it with (for example, array number 100):
NSMutableArray *uniqueArray = arrayStore[#100];

How to remove duplicity from array of dictionaries in IOS? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
(
{
color = blue;
},
{
color = blue;
},
{
color = red;
},
{
color = white;
}
)
This is an Array of dictionary, i have to remove duplicate dictionary from array corresponding to key color.
NSSet is to save you in this case. Use:
NSSet *set = [NSSet setWithArray:duplicateArray];
NSArray *uniqueArray = [set allObjects];
Avoid using loop for this because if you have more object loop is a consuming process. You can directly use NSSet and it will work for sure.
Working Code :
NSArray *html = #[#{#"color": #("blue")},#{#"color": #("blue")},#{#"color": #("red")},#{#"color": #("yellow")}];
NSMutableArray *finalArray = [NSMutableArray array];
NSMutableSet *mainSet = [NSMutableSet set];
for (NSDictionary *item in html) {
//Extract the part of the dictionary that you want to be unique:
NSDictionary *dict = [item dictionaryWithValuesForKeys:#[#"color"]];
if ([mainSet containsObject:dict]) {
continue;
}
[mainSet addObject:dict];
[finalArray addObject:item];
}
NSLog(#"%#", finalArray);
An alternative to Vin's solution which I believe would work. But this one does not create a resulting array. It manipulates the existing one. For doing so it creates temporary copies to drive the iterations.
NSArray workingCopy = [NSArray arrayWithArray:yourArray];
for (int i = 0; i < [workingCopy count] - 1; i++) { // count - 1 just saves time. Works nicely without.
for (int j = i+1; j < [workingCopy count]; j++) {
if ([[[workingCopy objectAtIndex:i] objectForKey:#"color"] isEqualToString: [[workingCopy objectAtIndex:j] objectForKey:#"color"]] {
[yourArray removeOjbect:[[workingCopy objectAtIndex:i] objectForKey:#"color"]] // yourArray must be mutable for this.
}
}
}
This algo creates a copy of the original array before. That is to avoid hasseling with changes to the very array that is used for iterations/enumerations. Then it iterates though the copy in a 2-dimensional loop by avoiding to compare the same object with itself (i is never qual to j) and it avoids compaing A with B when B was already compared with A. Both is achieved by stating the j loop with i+1.
The very last iteration would be i = [workingCopy count]. Then j would start off with i+1 and therefore already be larger than [workingCopy count]. The loop's body would not be executed a single time. That's why the i loop can already finish with [workingCopy count] - 1.
The same can be achieved without a copy of the original array. But that does require rather smart manipulations of the running idices i and j, which is no good programming style, rather complex and error prone.
if arr is the array from which you want to remove duplicates
for(int index = 0;index<arr.count;index++){
NSDictionary *dict = [arr objectAtIndex:index];
for(int i = index-1 ; i>=0 ;i++){
NSDictionary *dictToComp = [arr objectAtIndex:i];
if([[dict objectForKey:#"color"] isEqualToString:[dictToComp objectForKey:#"color"]]){
[arr removeObject:dict];
}
}
}

Want to store NSRange and NSInteger inside NSMutableDictionry [duplicate]

This question already has answers here:
Passing NSInteger variable to NSMutableDictionary or NSMutableArray
(7 answers)
Closed 9 years ago.
I need to store both the NSRange object and NSInteger inside the NSMutableDictionary?
Can I do it?
If it is, Could you give me an example?
You can store the NSInteger as NSNumber object.
mutableDictionary[integerKey] = [NSNumber numberWithInteger:integer];
or even better
mutableDictionary[integerKey] = #(integer);
For NSRange, use NSValue object.
mutableDictionary[rangeKey] = [NSValue valueWithRange:range];
I have used NSString for both NSRange and NSInteger, to remove confusion use this:
NSInteger i = 6;
NSRange range = NSMakeRange (25, 3);
NSMutableDictionary *dic =[[NSMutableDictionary alloc] init];
[dic setObject:NSStringFromRange(range) forKey:#"range"];
[dic setObject:[NSString stringWithFormat:#"%d",i] forKey:#"integer"];
//How to get it...
NSRange range1 = NSRangeFromString([dic objectForKey:#"range"]) ;
NSInteger inte = [[dic objectForKey:#"integer"] integerValue];

Convert char to int to NSString

I am writing some code to allow users to answer multiple choice questions. So I have an NSArray of values [#"value1", #"value2", ..]
I want to display them as:
A) value1
B) value2
The code I have is
for(int i = i; i < [values count]; i = i+1) {
NSString *displayValue = [[NSString alloc] initWithString:<NEED HELP HERE>];
displayValue = [displayValue stringByAppendingString:#") "];
displayValue = [displayValue stringByAppendingString:[values objectAtIndex:i];
}
The question I have is if there is where I have put , how could I convert i to the right ASCII character (A, B, C, etc) and initialize the string with that value
NSString *displayValue = [NSString stringWithFormat:#"%c",'A'-1+i];
and to get the whole string at once, use:
NSString *displayValue = [NSString stringWithFormat:#"%c) %#",'A'-1+i,
[values objectAtIndex:i]];
(ps. if you alloc an object, you must also release or autorelease, or you will "leak" memory)
Look at NSString:initWithFormat method, along with the String Programming Guide.

Filter a NSMutableArray index beyond bounds Problem

I am trying to filter a NSMutableArray. Search array for items by certain country. I have tried NSpredicate which works great however I need to re-use the original array which I cannot with NSpredicate. So I am trying the below code.
Q. What is the best way to filter an NSMutableArray keeping the original array intact?
//The following code works
filteredArray = [[NSMutableArray alloc] init];
unsigned int i;
for (i = 0; i < [appDelegate.arrayToBeFiltered count]; i++) {
id session = [appDelegate.ads objectAtIndex: i];
id Country = [[appDelegate.arrayToBeFiltered objectAtIndex: i] TheCountry];
[filteredArray addObject: session];
However when I add the if statement as below I get index beyond bounds
filteredArray = [[NSMutableArray alloc] init];
unsigned int i;
for (i = 0; i < [appDelegate.arrayToBeFiltered count]; i++) {
id session = [appDelegate.ads objectAtIndex: i];
id Country = [[appDelegate.arrayToBeFiltered objectAtIndex: i] TheCountry];
if (Country == #"United States"){
[filteredArray addObject: session];
}
}
Use - (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate
As a side-note, in the code above you probably mean to do this [Country isEqualToString: #"United States"]
As an extra side note - don't capitalise variables and method names. Just a style thing but capitalisation is usually reserved for Class names