Ok. i don't see ways to done it. But maybe some one knows?
I don't need use NSString to put it in array, because it makes huge memory usage.
NSMutableArray *array = #[#(-16.f)].mutableCopy;
for (NSNumber *number in array) {
NSLog(#"%f", number.floatValue);
}
first you need to convert yourfloat value toNSNumber using #(floatValue) .
NSMutableArray *arr = [[NSMutableArray alloc]init]; // here you can add value using initWithObject method.
[arr addObject:#(-16)]; // here you can add your float value using #(float value).
NSLog(#" array = %#",arr);
When you retrive value from array .
float yourValue = [[arr objectAtIndex:(your index)] floatValue];
hope it help you .
Related
I have a NSMutableArray called myMutableArray inside him, I have the following values:
('R$ 118.98','AE 12.00 er','R$ 456.99')
What I would do, is find a way to filter the information contained within this array, thus making it returns only numeric characters, for example:
('118.98','12.00','456.99')
I have a simple code who get the lines inside an array:
for(int x=0; x<[myMutableArray count]; x++){
myMutableArray[x];//We need to find a way to filter and update this informations to only store numbers.
}
What the code I can put in my code to filter the information inside my array to only storing numbers?
Try:
//create a new mutable array to store the modified values
NSMutableArray *arrFinal = [[NSMutableArray alloc] init];
//fast-enumerate within myMutableArray
for (NSString *strCurrent in myMutableArray) {
NSString *strModified = [strCurrent stringByTrimmingCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
[arrFinal addObject:strModified];
}
NSLog(#"%#",[arrFinal description]);
I feel like I have a unique problem. This problem stems from parsing a COLLADA XML file. I am filtering by whitespace in the element of the file, and it's working fine, except when I dump that to an NSArray, I get a bunch of junk zero values before I hit the actual points I am trying to isolate.
My question is… If my NSMutableArray has a bunch of NSNumber values, and I want to filter out all zero values, how can I do this?
I thought I could do something like:
NSNumber * newNumber;
NSMutableArray * newArray = [[NSMutableArray alloc] init];
for(oldNumber in oldMutableArray)
{
if(oldNumber != 0)
{
[newArray addObject: oldNumber];
}
}
This does not work, however. :)
Your main issue here is that != 0 is really checking for nil values, not if the NSNumber is equal to 0.
You should be able to just do this:
[allXMLObjects removeObject:#(0)];
This will remove all occurrences of NSNumbers that have a numerical value of 0.
Add oldNumber, not newNumber. newNumber was nil
NSMutableArray * newarray = [[NSMutableArray alloc] init];
for(oldNumber in oldmutablearray)
{
if(oldnumbervalues != 0)
{
[newarray addObject: oldNumber]; //Add old number, not newNumber. newNumber was nil
}
}
I'm new in Cocoa.
I have NSString - (e.g) MUSIC . I want to add some new NSString in Array,
And want to check something like this
if MUSIC already contained in Array, add Music_1 , after Music_2 and so on.
So I need to be able read that integer from NSString, and append it +1 .
Thanks
Use
NSString *newString = [myString stringByAppendingString:[NSString stringWithFormat:#"_%i", myInteger]];
if myString is "music", newString will be "music_1" or whatever myInteger is.
EDIT: I seem to have gotten the opposite meaning from the other answer provided. Can you maybe clarify what it is you are asking exactly?
Check it out:
NSMutableArray *array = [NSMutableArray arrayWithObjects:#"123", #"qqq", nil];
NSString *myString = #"MUSIC";
NSInteger counter = 0;
if ([array containsObject:myString]){
NSString *newString = [NSString stringWithFormat:#"%#_%d", myString, ++counter];
[array addObject:newString];
}
else
[array addObject:myString];
For checking duplicate element in Array you can use -containsObject: method.
[myArray containsObject:myobject];
If you have very big array keep an NSMutableSet alongside the array.Check the set for the existence of the item before adding to the array. If it's already in the set, don't add it. If not, add it to both.
If you want unique objects and don't care about insertion order, then don't use the array at all, just use the Set. NSMutableSet is a more efficient container.
For reading integer from NSString you can use intValue method.
[myString intValue];
For appending string with number you can use - (NSString *)stringByAppendingString:(NSString *)aString or - (NSString *)stringByAppendingFormat:(NSString *)format ... method.
Here's how you convert a string to an int
NSString *myStringContainingInt = #"5";
int myInt = [myStringContainingInt intValue];
myInt += 1;
// So on...
I have a UIPickerView with three components, and each component has NSIntegerMax for the numbers. So, I just need to get the data from each component, and send it to another ViewController called, CreationViewController. The three objects I need to send are strings, set up like so:
NSInteger supplyData = [supplypick selectedRowInComponent:0];
NSInteger mineralData = [supplypick selectedRowInComponent:1];
NSInteger vespeneData = [supplypick selectedRowInComponent:2];
So, I would like to add each cell in the format of this log:
NSLog(#"%i %# M:%i G:%i", supplyData, namer.text, mineralData, vespeneData);
All I need to know it what to put...
NSMutableArray * array = [[NSMutableArray alloc] init];
[array addObject: ];
^^HERE^^
This is all in one function. Please help! Thanks!
[array addObject:[NSString stringWithFormat:#"%i %# M:%i G:%i", supplyData, namer.text, mineralData, vespeneData]];
I want to ask about the objective C question. I want to create a 2D NSArray or NSMutableArray in objective C. What should I do? The object stored in the array is NSString *. Thank you very mcuh.
This is certainly possible, but i think it's worthy to note that NSArrays can only hold objects, not primitive types.
The way to get around this is to use the primitive wrapper type NSNumber.
NSMutableArray *outer = [[NSMutableArray alloc] init];
NSMutableArray *inner = [[NSMutableArray alloc] init];
[inner addObject:[NSNumber numberWithInt:someInt]];
[outer addObject:inner];
[inner release];
//do something with outer here...
//clean up
[outer release];
Try NSMutableDictionary with NSNumbers as keys and arrays as objects. One dimension will be the keys, the other one will be the objects.
To create the specific "2D array"
NSMutableDictionary *twoDArray = [NSMutableDictionary dictionary];
for (int i = 0; i < 10; i++){
[twoDArray setObject:arrayOfStrings forKey:[NSNumber numberWithInt:i]];
}
To pull the data
NSString *string = [[twoDArray objectForKey:[NSNumber numberWithInt:3]] objectAtIndex:5];
//will pull string from row 3 column 5 -> as an example
Edited to make my answer more applicable to the question. Initially I didn't notice that you were looking for a 2D array. If you know how many by how many you need up front you can interleave the data and have a stride. I know that there are probably other (more objective standard) ways of having arrays inside of an array but to me that gets confusing. An array inside of an array is not a 2 dimensional array. It's just a second dimension in ONE of the objects. You'd have to add an array to each object, and that's not what I think of as a 2 dimensional array. Right or wrong I usually do things in a way that makes sense to me.
So lets say you need a 6x6 array:
int arrayStride=6;
int arrayDepth=6;
NSMutableArray *newArray = [[NSMutableArray alloc] initWithCapacity:arrayStride*arrayDepth];
I prefer to initialize the array by filling it up with objects;
for(int i=0; i<arrayStride*arrayDepth; i++) [newArray addObject #"whatever"];
Then after that you can access objects by firstDim + secondDim*6
int firstDim = 4;
int secondDim = 2;
NSString *nextString = [newArray objectAtIndex:firstDim+secondDim*6];