NSMutableArray count is 0 after initWithObject - ios

I've defined a property of NSMutableArray. Right after calling initWithObjects, it's count is 0. What am I missing here? Here's my code:
#interface MainViewController ()
#property NSMutableArray *photos;
#end
- (void)viewDidLoad {
[super viewDidLoad];
_photos = [_photos initWithObjects:#"1", #"2", #"3", nil];
NSLog(#"Photos count: %lu",(unsigned long)_photos.count); //prints 0
}

This is because _photos is nil at the time that you are trying to call the init method. Use arrayWithObjects instead:
_photos = [NSMutableArray arrayWithObjects:#"1", #"2", #"3", nil];

It is illegal to say init... without saying alloc in the same line. alloc is a class method, and your class is NSMutableArray, so you should have said:
_photos = [[NSMutableArray alloc] initWithObjects:#"1", #"2", #"3", nil];

Related

How to replace NSMutableArray List data

I am very new for IOS. In my app, I have inserted two UIButtons on my mainViewController
When I click first button I am adding some data in my Main NSMutableArray and when I click "second" button I want remove previous array details and replace new data.
My code:
- (void)viewDidLoad {
[super viewDidLoad];
mainArray = [[NSMutableArray alloc] init];
}
- (IBAction)button1:(id)sender{
NSMutableArray * data1 = [[NSMutableArray alloc]
initWithObjects:#"1",#"2",#"3", nil];
[mainArray replacementObject:data1];
}
- (IBAction)button2:(id)sender{
NSMutableArray * data2 = [[NSMutableArray alloc]
initWithObjects:#"1",#"2",#"3",#"4",#"5", nil];
[mainArray removeObjectAtIndex:data2];
}
Please help.
Try this code
- (IBAction)button1:(id)sender{
NSMutableArray * data1 = [[NSMutableArray alloc]
initWithObjects:#"1",#"2",#"3", nil];
[mainArray addObject:data1];
}
- (IBAction)button2:(id)sender{
NSMutableArray * data2 = [[NSMutableArray alloc]
initWithObjects:#"1",#"2",#"3",#"4",#"5", nil];
[mainArray replaceObjectAtIndex:0 withObject:data2];
}
I assumed your mainArray contains only one element. If there are more elements you need to specify correct index
Try This one:
- (IBAction)button1:(id)sender{
[mainArray addObjectsFromArray:[[NSMutableArray alloc]
initWithObjects:#"1",#"2",#"3", nil]];
}
- (IBAction)button2:(id)sender{
[mainArray replaceObjectsInRange:NSMakeRange(0, mainArray.count) withObjectsFromArray:[[NSMutableArray alloc]
initWithObjects:#"6",#"2",#"3",#"4",#"5", nil]];
}
- (void)viewDidLoad {
[super viewDidLoad];
mainArray = [[NSMutableArray alloc]init];
}
- (IBAction)button1:(id)sender{
mainArray = [[NSMutableArray alloc]
initWithObjects:#"1",#"2",#"3", nil];
}
- (IBAction)button2:(id)sender{
mainArray = [[NSMutableArray alloc]
initWithObjects:#"1",#"2",#"3",#"4",#"5", nil];
}

How to access an instance variable by an NSString generated at run-time (reflection)

I've got the following little code conundrum..
- (void) transmitArray {
NSString* arrayName = #"array1";
NSArray* array1 = [NSArray arrayWithObject:#"This is Array 1"];
NSArray* array2 = [NSArray arrayWithObject:#"This is Array 2"];
NSMutableArray* targetArray = [NSMutableArray arrayWithCapacity:1];
}
Is there a way to use the string "array1" to access the NSArray 'array1' so I can copy it into the target array.. And therefore, if the string read "array2", I'd be able to access the second array?
I read about using NSSelectorFromString to create a selector, and that this technique was called 'introspection', but other than that I'm stumped..
Hope someone can help!
Not really. If it were a instance variable of the class (generally known as a property) then you could use introspection to inspect the object and get/set the needed variables. You can can use the KVO methods
setValue:(id) ForKeyPath:(NSString *)
and
valueForKeyPath:(NSString *)
to access them
However you can't do that with local variables declared inside of an instance method (directly). I would suggest populating a dictionary with your arrays and then using it as a lookup table
NSMutableDictionary *arrays = [[NSMutableDictionary alloc] init];
NSArray *array1 = #[#"This is Array 1"];
[arrays setObject:array1 forKey:#"array1"];
NSArray *array2 = #[#"This is Array 2"];
[arrays setObject:array1 forKey:#"array2"];
//grab a copy of array1
NSArray *targetArray = [arrays[#"array1"] copy];
If you have array1 declared as a property you can do it like this:
#import "ViewController.h"
#interface ViewController ()
#property NSArray *array1;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.array1 = [NSArray arrayWithObjects:#"Hello", #"world!", nil];
NSArray *array2 = [self valueForKeyPath:#"array1"];
NSLog(#"%#", array2);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end

Add object to NSMutableArray in another class

Hi I have a grouped tableview the first section contains a list of emails and the second section just has two rows which are add email manually and select email from contacts.
The log in ManualEmail.m keeps logging 0 for the count and the array in EmailViewController is never modified, but I can't figure out what's wrong
This is my current set up
EmailViewController.h
#property (nonatomic, retain) IBOutlet NSMutableArray *dataArray;
EmailViewController.m
#synthesize dataArray;
- (void)viewDidLoad {
[super viewDidLoad];
dataArray = [[NSMutableArray alloc] init];
NSMutableArray *listItems = [[NSMutableArray alloc] initWithObjects:nil];
[listItems addObject:[ObjectArrays productWithType:#"test" Eemail:#"test#website.com" Eselected:YES]];
NSDictionary *firstItemsArrayDict = [NSDictionary dictionaryWithObject:listItems forKey:#"data"];
[dataArray addObject:firstItemsArrayDict];
NSArray *secondItemsArray = [[NSArray alloc] initWithObjects:#"Add Email Address From Contacts", #"Add Email Address Manually", nil];
NSDictionary *secondItemsArrayDict = [NSDictionary dictionaryWithObject:secondItemsArray forKey:#"data"];
[dataArray addObject:secondItemsArrayDict];
[tableView reloadData];
}
ManualEmail.m
EmailViewController *emailPVC = [[EmailViewController alloc] init];
NSDictionary *dictionary = [emailPVC.dataArray objectAtIndex:0];
NSArray *array = [dictionary objectForKey:#"data"];
NSMutableArray *emailArray = [NSMutableArray arrayWithArray:array];
[emailArray addObject:[ObjectArrays productWithType:name.text Eemail:email.text Eselected:YES]];
[emailPVC.dataArray removeObjectAtIndex:0];
NSDictionary *firstItemsArrayDict = [NSDictionary dictionaryWithObject:emailArray forKey:#"data"];
[emailPVC.dataArray insertObject:firstItemsArrayDict atIndex:0];
NSLog(#"%d", [emailPVC.dataArray count]);
In ManualEmail.m, you're creating the EmailViewController in code. But you aren't ever calling anything that would call its -viewDidLoad method, so the dataArray isn't ever getting created or filled out. You need to call emailPVC.view = <some view you created>; in order for -viewDidLoad to get called.

iOS:Retrieve data from function input parameter

I want to know how to retrieve the values by giving input parameter to a function. I want to retrieve in this way here as below, but it doesn't retrieve it rather giving empty array value when printing. I can't use even ampersand like c in parameter.
NSMutableArray *firstArray = [[NSMutableArray alloc] init];
NSMutableArray *secondArray = [[NSMutableArray alloc] init];
MyClass *myClassObj = [[MyClass alloc] init];
[myClassObj getVals : firstArray : secondArray];
NSLog(#"firstArray: %#", firstArray); // empty
NSLog(#"secondArray: %#", secondArray); // empty
// function to retrieve
- (void) getVals :(NSMutableArray *) firstArray :(NSMutableArray *) secondArray
{
firstArray = [NSMutableArray arrayWithObjects:#"val1", #"val2", #"val3", #"val4", nil];
secondArray = [NSMutableArray arrayWithObjects:#"val11", #"val22", #"val33", #"val44", nil];
}
Try this:
- (void) getVals :(NSMutableArray *) firstArray :(NSMutableArray *) secondArray {
[firstArray addObjectsFromArray:#[ #"val1", #"val2", #"val3", #"val4" ]];
[secondArray addObjectsFromArray:#[ #"val11", #"val22", #"val33", #"val44" ]];
}
The code you have assigns a new array to the local parameter values instead of adding values to the passed in arrays.
Your other option could be:
NSMutableArray *firstArray = nil;
NSMutableArray *secondArray = nil;
MyClass *myClassObj = [[MyClass alloc] init];
[myClassObj getVals : &firstArray : &secondArray];
- (void) getVals :(NSMutableArray **) firstArray :(NSMutableArray **) secondArray {
*firstArray = [NSMutableArray arrayWithObjects:#"val1", #"val2", #"val3", #"val4", nil];
*secondArray = [NSMutableArray arrayWithObjects:#"val11", #"val22", #"val33", #"val44", nil];
}
You are overriding the object reference with new object. U should rather use
[firstArray addObject:#"val1"]

How to return all Array value from other class

How to return all Array value if it created and initial in separate class of type NSMutableArray.
This is my simple code.
In FirstClassViewController
- (void)viewDidLoad
{
[super viewDidLoad];
MyArrayClass *thisArray = [[MyArrayClass alloc]init];
NSLog(#"%#",thisArray.getArray);
}
In MyArrayClass.h
#interface MyArrayClass : NSMutableArray{
NSMutableArray *courseArray;
}
-(NSString*)getArray;
-(void) setArray;
#end
In MyArrayClass.m
-(NSString*) getArray{
return courseArray;
}
-(void) setArray{
courseArray = [[NSMutableArray alloc]init];
// AR111:
[courseArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:
#"AR111",#"courseCode",
#"Arabic Communication Skills (I)",#"courseName",
#"3",#"creditHours",
nil]];
// AR112:
[courseArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:
#"AR112",#"courseCode",
#"Arabic Communication Skills (II)",#"courseName",
#"3",#"creditHours",
nil]];
}
I have this output message:
2012-07-19 10:36:18.344 test[622:f803] (null)

Resources