How to get last array object as int - ios

How can I get the last object from an array as an int, for example if I have 20 objects in the array, how can I get the last object as an int variable that = 20. Or if the array had 999 objects, the last object int would = 999.
How can I do this? I have tried using (int)array.lastobject but have had no luck, any help would be greatly appreciated.

It depends on what type of objects are being stored in the array? if they are NSNumber or NSString than you can do this:
int myVar = [[array lastObject] intValue];
Edit:
Your question is rather confusing it also seems that you want array count instead of last object? If thats the case than use this:
int myVar = [array count];

Option 1
int yourVariable = [[yourArray lastObject] intValue];
Option 2
int yourVariable = [[yourArray objectAtIndex:([yourArray count]-1)] intValue];

you can use lastObject method to get last object from array
int lastValue = [[yourArray lastObject] intValue];

Related

Array items showing error in IOS

I have an array with some items to rotate the image view when button is clicked, now when I pass the array with getting current index it showing an error, I'm confused why I'm getting this.
My code is this:
- (IBAction)my:(id)sender {
NSString *cureentIndex=0;
NSArray *persons = [NSArray arrayWithObjects:#"M_PI",#" M_PI_4",#" M_PI_2",#"M_PI*2", nil];
NSArray *person = #[#"M_PI", #"M_PI_4", #"M_PI_2"];
_imageView.transform = CGAffineTransformMakeRotation(person[cureentIndex])
if currentIndex != persons.count-1 {
currentIndex = currentIndex + 1
}else {
// reset the current index back to zero
currentIndex = 0
}
}
The error is here:
You have declared cureentIndex as NSString *, so when you say person[cureentIndex] the compiler thinks that person must be a dictionary, since you are using the [] access with an object. This causes the error since person is actually an array and it cannot be indexed with a string.
I think you meant to declare cureentIndex as int, or perhaps you meant to say currentIndex?
There are a lot of issues, please try this
NSInteger currentIndex = 0;
NSArray<NSNumber *> *persons = #[#(M_PI), #(M_PI_4), #(M_PI_2)];
- (IBAction)my:(id)sender {
_imageView.transform = CGAffineTransformMakeRotation(persons[currentIndex].doubleValue)
currentIndex = (currentIndex + 1) % persons.count;
}
There are some errors:
The index must be an int
NSString *cureentIndex=0;
becomes
int currentIndex = 0;
and the array must be of double not string, CGAffineTransformMakeRotation requires a CGFloat that is a double
NSArray *person = #[#"M_PI", #"M_PI_4", #"M_PI_2"];
becomes
NSArray *person = #[[NSNumber numberWithDouble:M_PI], [NSNumber numberWithDouble:M_PI_4], [NSNumber numberWithDouble:M_PI_2]];
and
_imageView.transform = CGAffineTransformMakeRotation(person[cureentIndex])
becomes
_imageView.transform = CGAffineTransformMakeRotation([person[cureentIndex] doubleValue]);
I don't know why you set currentIndex's property to NSString, In fact we always set the XXXindex(current/last/next) to int or NSInteger property.
Another, the person is an array, it's key must be an int!, it's
value could be any object.
I fix your code to this:
NSInteger currentIndex = 0;
NSArray *person = #[#"M_PI", #"M_PI_4", #"M_PI_2"];
CGFloat rotate = [person[currentIndex] floatValue];
_imageView.transform = CGAffineTransformMakeRotation(rotate);
you will find it did't work well, the ratate will be 0! Because, the NSString's method floatValue or doubleValue can only change string(like: #"123" #"0.5") to a number, the string(like: #"M_PI") can't be change to a right number.
I fix your code to this again:
NSInteger currentIndex = 0;
CGFloat mPi = M_PI;
NSArray *person = #[#(mPi), #(mPi / 4.0), #(mPi / 2.0)];
CGFloat rotate = [person[currentIndex] floatValue];
_imageView.transform = CGAffineTransformMakeRotation(rotate);
The Code works well! This is because M_PI is A Macro, if you
write code #"M_PI", the system can't recognize its value 3.1415926. So
your must write M_PI instead of #"M_PI".
In fact, this problem's core is you need a number, so your array must include some numbers! Or some string just like number! :)

Loading NSMutableArray objects into an IBCollection of Labels

My problem is that when I use fast enumeration to load objects from my array, like so:
for(SetOfObjects *set in _myArray){
NSLog (#"%#"[set anObject];
}
It will print out my specified object without a problem, however when it comes time to assign these objects to an NSArray of labels. The last object returns as 0.
Like so:
for(SetOfObjects *set in _myArray){
for(UILabel *label in _arrayOfLabels){
int i = [set intObject];
NSString *string = [NSString stringWithFormat:#"%i",i];
label.text = string;
}
}
I think, I have gone wrong here. The code works, but the problem is that all labels are then set as 0.
Any tips welcome.
You are iterating the labels within each SetOfObjects instance, when in fact you want to iterate both arrays at the same time, which cannot be done using fast enumeration.
Instead revert to indexed-access of both arrays:
NSInteger count = [_myArray count];
NSAssert([_arrayOfLabels count] == count, #"Different array sizes!");
for (NSInteger index = 0; index < count; index++) {
SetOfObjects *set = _myArray[index];
UILabel *label = _arrayOfLabels[index];
int i = [set intObject];
NSString *string = [NSString stringWithFormat:#"%i",i];
label.text = string;
}
Note the assertion to check that both arrays are the same size.
EDIT: Oops, i was a bad variable name to choose for the index...

NSMutableArray, checking which Value is the most abundant?

How can I check which value in an NSMutableArray is the most frequent?
Array = "0,2,3,2,2,4
the value = "2".
Take a look at NSCountedSet this will help with your problem.
NSCountedSet *countedSet = [[NSCountedSet alloc] initWithArray:youArray];
NSInteger maxCount = 0;
id maxObject = nil;
for (id object in countedSet) {
if ([countedSet object] > maxCount) {
maxCount = [countedSet countForObject:object];
maxObject = object;
}
}
return maxObject;
This does sound like homework though.
EDIT
If they are stored as strings instead of numbers then swap out NSNumber for NSString everything else works the same.
EDIT
Actually, I just realised that it doesn't care about what object type it is...
Latest edit will work whatever the object is.

Handle NSNumber and NSInteger

Following is a code snippet i am using to add data to nsmutable array, now I am not sure on what to type cast it on while extracting, i need integer value.
Problem is that I am getting warnings of 'id' and 'NSInteger' conversion. What could be better way of extracting:
self.itemsBottom = [NSMutableArray array];
for (int i = 20; i < 30; i++)
{
[itemsBottom addObject:#(i)];
}
wanna do something like:
NSInteger itemAddressed = [self.itemsBottom objectAtIndex:itemIndex]
In this statement
[itemsBottom addObject:#(i)];
you are boxing the integer value to NSNumber.
While here
NSInteger itemAddressed = [self.itemsBottom objectAtIndex:itemIndex]
you are tried to store NSNumber to NSInteger, hence getting the error.
You can use :
NSInteger itemAddressed = [[self.itemsBottom objectAtIndex:itemIndex] integerValue];
Or in short :
NSInteger itemAddressed = [self.itemsBottom[itemIndex] integerValue];
All seems reasonable...I would think the last line would need to be...
NSInteger itemAddressed = [self.itemsBottom[itemIndex] integerValue];
Maybe?

How can an int be converted into an id?

I have an array, and the contents of it are objects of type id, but I need to turn them into type int. Is there any way I can make the array read the data as ints, or turn the id into an int?
NSArray *array = [string componentsSeparatedByString:#"\n"];
int foo = array[0]; /*Warning: Incompatible pointer to integer conversion initializing 'int' with an expression of type 'id' */
componentsSeparatedByString: docs says:
Return value
An NSArray object containing substrings from the receiver that have been divided by separator.
So your fileContents contains an array of NSStrings. fileContents[0] is then the first NSString instance in the array. And you can convert NSString to int or preferably NSInteger by calling
[string intValue];
[string integerValue];
So your code should look like this (assuming array contains at least 1 object, don't forget to check this):
int object1 = [fileContents[0] intValue];
Or even better include typecasting for better code readability
int object1 = [(NSString *)fileContents[0] intValue];
You should use intValue to convert to int.
int object1 = [fileContents[0] intValue];

Resources