iOS Accessing Dictionary - ios

I have a plist which looks like this. I usually access using the following lines of code:
NSMutableDictionary *plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
CGFloat redFloat = (CGFloat)[[plistDict objectForKey:#"red"] floatValue];
However as it is nested - how do I access the colour red in BackgroundColour?
Thanks.

Absolutely: reading plist into a dictionary gives you a dictionary of dictionaries, so you need to access BackgroundColour first:
CGFloat redFloat = (CGFloat)[[[plistDict objectForKey:#"BackgroundColour"] objectForKey:#"red"] floatValue];
New syntax lets you write it with fewer characters:
CGFloat redFloat = (CGFloat)[plistDict[#"BackgroundColour"][#"red"] floatValue];

You can use valueForKeyPath: to shortcut too:
CGFloat redFloat = (CGFloat)[[plistDict valueForKeyPath:#"BackgroundColour.red"] floatValue];

CGFloat redFloat = [[[plistDict objectForKey:#"BackgroundColour"] objectForKey:#"red"] floatValue];

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! :)

Assign the value from a text field into an int variable to do some math with other variables and then return it?

The code showing here assigns the value of an variables to the text field I want to do the opposite of that.
_Initial.text = [NSString stringWithFormat:#"%f", init];
The opposite of the code above. Please help
The opposite of that would be getting converting string to float. You can do it with this
init = [_Initial.text floatValue];
You should search for an answer before you ask a question.
NSString *intString = #"123";
NSString *floatString = #"456.789";
int intVal = [intString intValue];
float floatVal = [floatString floatValue];
printf("%i %f\n",intVal,floatVal);

How We store Float value in NSMutableDictionary

- (void)buttonSave:(UIButton *)bttnSave
{
UILabel *lbl = (UILabel *)[self.view viewWithTag:kTagLblText];
[dicStore setValue:lbl.textColor forKey:#"Text Color"];
// fltValue is float type
[dicStore setObject:fltValue forKey:#"font"];
[dicStore setValue:strtextView forKey:#"Text Style"];
[arrStoreDic addObject:dicStore];
}
If We can store this then any one can help me please do
Simply:
dicStore[#"font"] = #(fltValue);
However it's troubling that dicStore is an instance variable that you want to store into an array, which leads me to believe you will end up with an array of the same dictionary, containing the same values.
Instead create a new dictionary each time and store it into the array (unless, of course, other code needs to see this dictionary). The current implementation is very brittle.
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:
[NSArray arrayWithObjects:[NSNumber numberWithFloat:0.5], [NSNumber numberWithFloat:0.7], [NSNumber numberWithFloat:0.2], nil],
nil];
The above code is used to add float values in Dictionary
NSMutableDictionary, NSarray, NSSet and other collection classes accept only objects, and a float variable is a primitive. You have to create a NSNumber object from your float :
NSNumber * floatObject =[NSNumber numberWithFloat:fltValue]
[dicStore setObject:floatObject forKey:#"font"];
Using literals, your code can be simplified to :
-(void)buttonSave:(UIButton *)bttnSave
{
UILabel *lbl = (UILabel *)[self.view viewWithTag:kTagLblText];
dicStore[#"Text Color"] = lbl.textColor;
dicStore[#"font"] = #(fltValue);
dicStore[#"Text Style"] = strtextView;
[arrStoreDic addObject:dicStore];
}
To get back the float value, it's pretty simple :
NSNumber * floatNumber = dicStore[#"font"];
float floatValue = floatNumber.floatValue;
NSDictionary *dict = #{#"floatProperty":[NSNumber numberWithFloat:floatValue]};

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.

How to find the smallest and biggest object of an NSMutableArray

Hi I am trying to make it so that I can find and log the biggest object of an NSMutableArray. I have searched for this, but I have only seen it for an NSArray. This is the code I saw.
numbers = [numbers sortedArrayUsingSelector:#selector(compare:)];
float min = [numbers[0] floatValue]
float max = [[numbers lastObject] floatValue];
However, when I put that in, it doesn't work because I am using a mutable array.
Thanks!
It works for mutable array.
NSArray *sortedNumbers = [numbers sortedArrayUsingSelector:#selector(compare:)];
float min = [sortedNumbers[0] floatValue]
float max = [[sortedNumbers lastObject] floatValue];

Resources