how can i put the integerValue for textField into the array? - ios

#property NSMutableArray*textFieldsA;
#property NSMutableArray*textFieldsB;
#property NSMutableArray*textFieldsR;
#property NSMutableArray *operandArrayA;
#property NSMutableArray *operandArrayB;
_textFieldsA = #[self.textFieldA0, self.textFieldA1, self.textFieldA2, self.textFieldA3, self.textFieldA4, self.textFieldA5, self.textFieldA6, self.textFieldA7, self.textFieldA8];
_textFieldsB = #[self.textFieldB0, self.textFieldB1, self.textFieldB2, self.textFieldB3, self.textFieldB4, self.textFieldB5, self.textFieldB6, self.textFieldB7, self.textFieldB8];
_textFieldsR = #[self.textFieldR0, self.textFieldR1, self.textFieldR2, self.textFieldR3, self.textFieldR4, self.textFieldR5, self.textFieldR6, self.textFieldR7, self.textFieldR8];
for (int i = 0; i < 9; i++) {
_operandA = [((UITextField*)_textFieldsA[i]).text integerValue];
_operandB = [((UITextField*)_textFieldsB[i]).text integerValue];
[self.arithmetic setOperandA:_operandA operandB:_operandB operator:_operator];
_finalString = [NSString stringWithFormat:#"%d",[self.arithmetic result]];
((UITextField*)_textFieldsR[i]).text = _finalString;
}
how can i put the integerValue of
self.textFieldA0.text , self.textFieldA1.text , self.textFieldA3.text ........self.textFieldA8.text
in to the array (operandArrayA)?
i have tried
operandArrayA[i] = [((UITextField*)_textFieldsA[i]).text integerValue];
but it is not work, how should i do?

Unfortunately you can not add primitive datatypes in NSArray. You need to create object of NSNumber for integer value. Store this object to an array.
Like this:
NSNumber *number = #([((UITextField*)_textFieldsA[i]).text integerValue]);
operandArrayA[i] = number;

You need to alloc and initialize all the arrays..like below
operandArrayA=[[NSMutableArray alloc]init];
operandArrayB=[[NSMutableArray alloc]init];
//if u want to store strings
[operandArrayA addObject:((UITextField*)_textFieldsA[i]).text];
[operandArrayB addObject:((UITextField*)_textFieldsB[i]).text];
//(OR) if u want to store integer value then u need to convert to NSNumber
[operandArrayA addObject:[NSNumber numberWithInt:[((UITextField*)_textFieldsA[i]).text integerValue]]];
[operandArrayB addObject:[NSNumber numberWithInt:[((UITextField*)_textFieldsB[i]).text integerValue]]];
//(OR)
operandArrayA[i]=[NSNumber numberWithInt:[((UITextField*)_textFieldsA[i]).text integerValue]];
operandArrayB[i]=[NSNumber numberWithInt:[((UITextField*)_textFieldsB[i]).text integerValue]];
Hope it helps you..

Related

Sort 2 NSArrays By Date [duplicate]

This question already has answers here:
Sorting two NSArrays together side by side
(4 answers)
Closed 6 years ago.
I have 2 NSMutableArrays declared. One is filled with names and then another one is filled with string values of NSDate.
I want to sort both of them according to the date in the second one. For example if element 3 in the date array becomes element 0 I want the same to happen for the name array.
What is the easiest way to do this? I know how to sort the date array just not the corresponding name array!
(Objective-C Please!)
Sorting 2 arrays and keeping them in sync is a pain. You basically have to sort them by hand.
Probably the easiest way to do this is to create an array of dictionaries where each dictionary contains a data and a name.
Then sort the array of dictionaries by date.
EDIT:
Here is the code for creating custom objects containing a name and a date. There is code to sort by date as well as code to sort by name:
/**
The thing class has a property date and a property name.
It is purely for creating sorted arrays of objects
*/
#interface Thing : NSObject
#property (nonatomic, strong) NSDate *date;
#property (nonatomic, strong) NSString *name;
#end
#implementation Thing
/**
This is a dummy init method that creates a Thing ojbect with a random name and date
*/
- (instancetype) init {
self = [super init];
if (!self) return nil;
NSString *letters = #"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
NSMutableString *temp = [NSMutableString new];
for (int x = 0; x < 10; x++) {
unichar aChar = [letters characterAtIndex:arc4random_uniform(26)];
[temp appendFormat: #"%C", aChar];
}
self.name = [temp copy];
//Create a random date
uint32 halfMax = 2000000000;
uint32 max = halfMax * 2;
int32_t value = arc4random_uniform(max) - halfMax;
NSTimeInterval now = [[NSDate date] timeIntervalSinceReferenceDate];
self.date = [NSDate dateWithTimeIntervalSinceReferenceDate: now + value];
return self;
}
- (NSString *) description {
return [NSString stringWithFormat: #"Name: %# Date: %#", self.name, self.date];
}
#end
int main(int argc, const char * argv[]) {
#autoreleasepool {
//Create an array of Thing objects
const int count = 50;
NSMutableArray *thingArray = [NSMutableArray arrayWithCapacity: count];
for (int x = 0; x < count; x++) {
thingArray[x] = [[Thing alloc] init];
}
#if 1
//Sort by date, ascending
[thingArray sortUsingComparator:^NSComparisonResult(Thing *obj1,
Thing *obj2) {
NSComparisonResult bigger =
[obj1.date timeIntervalSinceReferenceDate] <
[obj2.date timeIntervalSinceReferenceDate] ?
NSOrderedAscending : NSOrderedDescending;
return bigger;
}];
#else
//Sort by name
[thingArray sortUsingComparator:^NSComparisonResult(Thing *obj1,
Thing *obj2) {
return [obj1.name compare: obj2.name];
}];
#endif
NSLog(#"%#", thingArray);
}
return 0;
}

Corebluetooth NSMutableArray with characteristic values

Using the setNotify=YES property I want to create an array populated with the converted 'characteristic.value' values from the below code (Xcode7.2). So far I've only got a null array, all the same values, or only one value at a time
I've implemented
#property (strong, nonatomic) NSMutableArray *voltageArray; //didn't work so
I deleted and tried the following...
The method that I tried to use it in was didUpdateCharacteristic,
if (characteristic.isNotifying)
{
NSString *stringFromData = [[NSString alloc] initWithData:characteristic.value
encoding:NSUTF8StringEncoding];
NSNumber *number = [NSNumber numberWithUnsignedShort:stringFromData];
number = #([number floatValue] *5/2034);
NSLog(#"Characteristic Value: %#", _voltageArray);
}
I also tried the following in the didUpdateValueMethod, along with countless other things,
_voltageArray=[[NSMutableArray alloc] init];
[self.voltageArray addObject:number];
NSLog(#"Array: %#", _voltageArray);
The best I've gotten is all the same value or all null. How do I get these unsigned voltage values into an array?
Updated Solution
#property (strong, nonatomic) NSMutableArray *voltageArray;
-(void)peripheral: didUpdateValueForCharacteristic: error:{
if (characteristic.isNotifying)
{
if(!self.voltageArray){//Checks if voltageArray has been initialised
self.voltageArray = [[NSMutableArray alloc] init];//If not, initialise the array
}
// generate characteristic value
NSString *stringFromData = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
unsigned short baseValue = (unsigned short)stringFromData;
NSNumber *number = [NSNumber numberWithUnsignedShort:baseValue];
number = #([number floatValue] *5/2034);
NSLog(#"Characteristic Value: %#", number);
// give potential recording
self.voltageLabel.text = [NSString stringWithFormat:#"%#",number];
// datasource points y-axis
[_voltageArray addObject:number];
NSLog(#"Array: %#", _voltageArray);
There are a couple of errors that may be causing your issue.
First:
NSString *stringFromData = [[NSString alloc] initWithData:characteristic.value
encoding:NSUTF8StringEncoding];
NSNumber *number = [NSNumber numberWithUnsignedShort:stringFromData];
number = #([number floatValue] *5/2034);
The first method requires a unsigned short type as an input, but you are passing a NSString. You could do this instead:
NSString *stringFromData = [[NSString alloc] initWithData:characteristic.value
encoding:NSUTF8StringEncoding];
unsigned short baseValue = (unsigned short)stringFromData.intValue;
float convertedValue = (float)baseValue *5.0/2034.0; //5.0/2034.0 -> so it typecasts the result value as a float, not int
NSNumber *number = [NSNumber numberWithFloat: convertedValue];
[self.voltageArray addObject:number];
Second, it seems that you are initialising your array every time the didUpdateValueForCharacteristic gets called. Try this instead:
-(void)peripheral: didUpdateValueForCharacteristic: error:{
if (characteristic.isNotifying)
{
if(!self.voltageArray){//Checks if voltageArray has been initialised
self.voltageArray = [[NSMutableArray alloc] init];//If not, initialise the array
}
NSString *stringFromData = [[NSString alloc] initWithData:characteristic.value
encoding:NSUTF8StringEncoding];
unsigned short baseValue = (unsigned short)stringFromData.intValue;
float convertedValue = (float)baseValue *5.0/2034.0;
NSNumber *number = [NSNumber numberWithFloat: convertedValue];
[self.voltageArray addObject:number];
}
}
Hope this helps.

Get NSDictionary with variable names as keys

Say I have n variables
NSNumber* A = #(1);
NSNumber* B = #(2);
NSNumber* C = #(3);
NSNumber* D = #(4);
NSNumber* E = #(5);
...
I need a dictionary like
{#"A":#(1), #"B":#(2), #"C":#(3), #"D":#(4), ... }
One can imagine a more cumbersome example that would be tedious to type out
I think saw a C style function for it but I can't remember. Something like NSDictionaryForVariables()
The C preprocessor macro (not a function) you're looking for is NSDictionaryOfVariableBindings. However, outside of Auto Layout (and, debatably, even there), it's not really a great idea to be setting up dependencies between runtime and compile-time identifiers like that.
Depending on what you're trying to actually accomplish, Key-Value Coding might be a better solution.
It's not a good approach you may find another way to solve your issue but if you want to have an idea about your requested solution here here my try
Our properties
#interface TestyViewController ()
#property (nonatomic) NSNumber* a;
#property (nonatomic) NSNumber* b;
#property (nonatomic) NSNumber* c;
#property (nonatomic) NSNumber* d;
#end
Set the values
- (void)viewDidLoad {
[super viewDidLoad];
self.a=#(1);
self.b=#(2);
self.c=#(3);
self.d=#(4);
}
Get our instance variables
-(NSArray *)propertyNames{
unsigned int propertyCount = 0;
objc_property_t * properties = class_copyPropertyList([self class], &propertyCount);
NSMutableArray * propertyNames = [NSMutableArray array];
for (unsigned int i = 0; i < propertyCount; ++i) {
objc_property_t property = properties[i];
const char * name = property_getName(property);
[propertyNames addObject:[NSString stringWithUTF8String:name]];
}
free(properties);
return propertyNames;
}
Create the dictionary
- (IBAction)buttonClicked:(id)sender
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
for (NSString* varName in [self propertyNames])
{
[dict setObject:[self valueForKey:varName] forKey:varName];
}
NSLog(#"%#",dict);
}
result
2015-07-15 20:30:56.546 TestC[879:27973] {
a = 1;
b = 2;
c = 3;
d = 4;
}

Add variable value with property name in IOS

I have 5 property with name address1, address2, ..... , address5. Now i want to iterate through all the property and assign them some value from array as follows.
for (int i=0; i<5; i++) {
self.address *value of i+1* = endAddress[i]; // like address1 = endAddress[0] and address2 = endAddress[1]
}
Ia there any way to achieve this? If yes, then how?
I googled it but unable to find a solution of adding integer with property instead found solutions to add integer with NSString.
Thanks in advance.
Key Value coding could be used for this, however the value being stored must be an Objective-C object, and not a primitive type. So for a number this means using NSNumber:
#property NSNumber *address1;
#property NSNumber *address2;
#property NSNumber *address3;
#property NSNumber *address4;
#property NSNumber *address5;
...
for (int i=0; i<5; i++) {
NSString *keyName = [NSString stringWithFormat:#"address%d", i + 1];
[self setValue:#(endAddress[i])
forKey:keyName];
}

Cannot sum values downloaded into array

I am working on a project where I download a few values (stored as float in my database) into my array and sum them. I can NSLog the numbers but Unfortunately I am having the hardest time to sum those values in my array.
Adding some code
DETAILS.H
#import <Foundation/Foundation.h>
#interface Details: NSObject
#property (nonatomic, strong)NSNumber *min;
//#property (nonatomic, strong) NSString *min;
#end
just the part of the code where I put the values in the array
for (int i = 0; i < jsonArray.count; i++)
{
NSDictionary *jsonElement = jsonArray[i];
tempDetails *downloadTemps = [[tempDetails alloc] init];
downloadTemps.min = jsonElement[#"min"];
// Add this question to the locations array
[_locations addObject:downloadTemps];
}
View controller code
for (Details *sav in _importArray )
{
NSLog(#"High :- %# ", sav.min); //THIS LIST ALL MY VALUES CORRECTLY
}
NSMutableArray *newArray = [_importArray mutableCopy];
int totalSum = 0;
for(int i=0; i<[newArray count];i++)
{
totalSum = totalSum + [[newArray objectAtIndex:i] intValue];
}
NSLog(#"Total:%d",totalSum);
In the view controller I get the error
[Details intValue]: unrecognized selector sent to instance ,
I am assuming I am getting this error because min is not declared right, But I am unsure how to do it other wise. Any help offered would be appreciated.
Thank you
You are asking for the selector intValue, but you should be asking for the selector min.
totalSum = totalSum + [[[newArray objectAtIndex:i] min] intValue];
You were pretty close in the first block you posted:
for (Details *sav in _importArray )
{
NSLog(#"High :- %# ", sav.min); //THIS LIST ALL MY VALUES CORRECTLY
}
Then:
int totalSum = 0;
for (Details *sav in _importArray )
{
totalSum += [sav.min intValue];
}
Incidentally, why are you asking for the intValue of something that you initially wrote was a float?

Resources