Array items showing error in IOS - 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! :)

Related

Objective-C: Randomizer objectAtIndex Not sorting through All Types

So I have a "food" table that has three types: "Meals, Desserts, and Snacks". I have these categories attached to buttons in a Search Options page and they basically do a "Select * from food where type = 'Meals'" kind of thing and that works fine.
The problem is that when I try to randomly retrieve an object from "All" ("select * from food") it only returns meals. I'm thinking it's a problem maybe with the "objectAtIndex:0"?
ranDom = [_entries bjl_shuffledArray];
Place *p = [ranDom objectAtIndex:0];
So I tried to randomize the index as well:
NSInteger randomIndex = arc4random()%[_entries count];
ranDom = [_entries bjl_shuffledArray];
Place *p = [ranDom objectAtIndex:randomIndex];
...but it still seems to only be retrieving results from Meals... not the other two types in "food".
Any idea how I can get it to better return results from all three types?
EDIT: Here's how bjl_shuffled_array works:
- (NSArray *)bjl_shuffledArray
{
NSMutableArray *shuffledArray = [self mutableCopy];
NSUInteger arrayCount = [shuffledArray count];
if (arrayCount > 0) {
for (NSUInteger i = arrayCount - 1; i > 0; i--) {
NSUInteger n = arc4random_uniform(i + 1);
[shuffledArray exchangeObjectAtIndex:i withObjectAtIndex:n];
}
}
return [shuffledArray copy];
}
So the "_entries" was only returning 30 results. I knew there had to be a limit somewhere because there should have been about 306 items. First I checked through all the code of the app for the number "30" and found nothing. So I checked through the PHP file and discovered that someone had snuck in this:
$rows_per_page = 30;
Changed "30" to "3000" and now all is right with the world. Thank you Rich Tolley for your help.

Inputted values from textfield to NSArray

I have a problem creating a solution to this problem:
Create an app that will store 5 numbers (preferrably float) and then sort them out.The array type is immutable.
First off: My problem is how to get the floats from the textfield then put it into a array. My idea is to code it like this:
int a = [num1.text intValue];
int b = [num2.text intValue];
int c = [num3.text intValue];
NSArray *myArray;
myArray = [NSArray stringWithFormat: #"%f",a,b,c];
My second problem is that I can't understand how to sort the floats. Will you please give me some idea?
Thank you very much!
Something like this should work:
CGFloat a = [num1Label.text floatValue];
CGFloat b = [num2Label.text floatValue];
CGFloat c = [num3Label.text floatValue];
CGFloat d = [num4Label.text floatValue];
CGFloat e = [num5Label.text floatValue];
NSArray *array = #[ [NSNumber numberWithFloat:a],
[NSNumber numberWithFloat:b],
[NSNumber numberWithFloat:c],
[NSNumber numberWithFloat:d],
[NSNumber numberWithFloat:e]];
array = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
// logic for sorting
NSNumber *number1 = (NSNumber *)obj1;
NSNumber *number2 = (NSNumber*)obj2;
return [first compare:second];
}];
CGFloat is basically the same as float.
If you check the Documentation (OPTION + Click on that), you see this:
# define CGFLOAT_TYPE float
// ...
typedef CGFLOAT_TYPE CGFloat;
CG comes from Core Graphics.
float comes from C/C++
It is more recommended to use CGFloat in Objective-C, instead of simply float. Also, NSInteger instead of int.

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?

Travel through NSArray without using loop but cursors?

I have an NSArray with objects inside (CLLocation). I can have 50, 100, 300 or more objects inside. In fact, this array is used while the user walking and can follow a direction. But the user can start at the middle of my NSArray, you know what I mean ?
Well, I Have to loop all the time my array to know where my user is compare to the locations in my array.
My question is: Is it possible to use a thing like in Java with a "cursor" in a list, and simply call "Next object" to travel in my array instead of loop ?
Because I need that the user walk on all location of my array.
Example:
Count of my array: 100
User start at location at index 34 (the nearest location found)
The user must do 35, 36, 37... 100 AND 0,1,2,3 ... until 33.
Hope it's clear, I really don't know how to do this without using for loop...
Thank you for help and suggestions!
Regards,
Lapinou.
Here's one way:
NSArray * arr = ... yourArray
int index = [arr indexOfObject:currentLocation];
index ++;
if (index == arr.count) index = 0;
id nextLocation = arr[index];
Another might be to create a global counter variable that stores the current position. If these needs to last after the user closes the app, you could write it to user defaults
Is looks like you are want to use NSEnumerator
NSEnumerator Class Reference
NSArray *anArray = // ... ;
NSEnumerator *enumerator = [anArray objectEnumerator];
id object;
while ((object = [enumerator nextObject])) {
// do something with object...
}
try this:
#interface ArrayEnumerator : NSEnumerator
{
NSArray* array;
NSInteger index;
NSInteger startIndex;
BOOL over;
}
- (id)initWithArray:(NSArray*)anArray
atIndex:(NSInteger)anIndex;
#end
#implementation ArrayEnumerator
- (id)initWithArray:(NSArray*)anArray
atIndex:(NSInteger)anIndex
{
if (self=[super init]) {
array = anArray;
index = anIndex;
startIndex = anIndex;
over = NO;
}
return self;
}
- (id)nextObject
{
if (index == [array count]) {
index = 0;
over = YES;
}
if (over && index == startIndex)
return nil;
return array[index++];
}
- (NSArray*)allObjects
{
return array;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
NSArray* array = #[#0,#1,#2,#3,#4,#5,#6,#7,#8,#9];
id element;
ArrayEnumerator* enumerator = [[ArrayEnumerator alloc] initWithArray:array atIndex:4];
while (element = [enumerator nextObject])
NSLog(#"%#", element);
}
#end
What is wrong with a for loop?
An iterator is typically used on lists, because you can't access elements in a list by index. However you are working with an array, so you don't need an iterator, but rather some clever way of accessing the array in the desired order.
Maybe this code can provide you with some ideas. This will run from 34 to 100, then start with 0 and go up to 33.
for (int i = 34; i < 134; i++)
{
int ix = i % 100;
id whatever = arr[ix];
}
You can access array elements by index:
CLLocation * myLocation = myArray[34];
(or)
int i = 34;
CLLocation * myLocation = myArray[i];

Resources