Access NSMutableArray from another class [duplicate] - objective-c

This question already has answers here:
Access NSMutableArray from a different class
(3 answers)
Closed 10 years ago.
This is how my Entity classes look:
BaseDataEntity.h
#import -Foundation/Foundation.h
#interface BaseDataEntity : NSObject
#property (assign) NSMutableArray *cbxDataList;
#property (assign) NSString *resaultText;
#end
BaseDataEntity.m
#import "BaseDataEntity.h"
#implementation BaseDataEntity
#synthesize cbxDataList = _cbxDataList;
#synthesize resaultText = _resaultText;
#end
And this is how I try to access them:
-(void)applicationDidFinishLaunching:(NSNotification *)aNotification{
BaseDataEntity *aBaseDataEntity = [[BaseDataEntity alloc] init];
[self setBaseDataEntity:aBaseDataEntity];
}
-(IBAction)ReadPort:(id)sender {
NSMutableArray* array = [[NSMutableArray alloc] init];
for (int i=0; i<10; i++) {
NSString *temp = [NSString stringWithFormat:#"Test %d", i];
[array addObject:temp];
}
[self.baseDataEntity setCbxDataList:array];
NSLog(#" cbxDataList count = %lu",[array count]);
[array release];
[self.baseDataEntity setResaultText:#"Test"];
[self updateUserInterface:TRUE];
}
-(void)updateUserInterface:(BOOL)piClear{
NSString *textValue = [NSString stringWithFormat:#"%#"
,[self.baseDataEntity resaultText]];
if([self.ResaultTextField stringValue] != nil && !piClear){
textValue = [NSString stringWithFormat:#"%#\n%#", textValue
,[self.ResaultTextField stringValue]];
}
NSMutableArray* array = [[NSMutableArray alloc] initWithArray:
[self.baseDataEntity.cbxDataList value]];
for (int i = 0; i < [array count]; i++) {
textValue = [NSString stringWithFormat:#"%#\n%#", textValue
,[array objectAtIndex:i]];
}
[array release];
[self.ResaultTextField setStringValue:textValue];
}
The problem is that I can update resaultText and have access to the value of this. But cbxDataList value is always empty.

You should use retain instead of assign to increase the retain count of cbxDataList.
#property (retain) NSMutableArray *cbxDataList;
Please note, that the object needs to be released sometime, or you could have a memory leak.

In the below function,
-(void)updateUserInterface:(BOOL)piClear
The array creation is wrong.
NSMutableArray* array = [[NSMutableArray alloc] initWithArray:
[self.baseDataEntity.cbxDataList value]];
The above code nneeds to be replaced by,
NSMutableArray* array = [[NSMutableArray alloc] initWithArray:
self.baseDataEntity.cbxDataList];

Try these :
#property (retain) NSMutableArray *cbxDataList;
#property (assign) NSString *resaultText;
And in -(void)applicationDidFinishLaunching:(NSNotification *)aNotification
alloc+init both of them.

Related

how to add objects to existing array in objective c

I am new to objective c and having some problem with nsmutableArray.I have button and two textfields on my gui and i want that when i click on button the strings from textfields should be added to my existing array. But the problem is that when i click on button it always create new array.Help anybody.
my button code in myfile.m is as follows:
NSMutableArray* myArray = [NSMutableArray array];
NSString *strr=[textf stringValue];
NSString *strr1=[textf1 stringValue];
// [myArray addObject:strr]; // same with float values
// [myArray addObject:strr1];
[myArray addObject:strr];
[myArray addObject:strr1];
int i,j=0;
int count;
for (i = 0, count = [myArray count]; i < count; ){
NSString *element = [myArray objectAtIndex:i];
NSLog(#"The element at index %d in the array is: %#", i, element);
}
Because you always create new array in this line:
NSMutableArray* myArray = [NSMutableArray array];
Make your array as property of your class object. Example:
#interface MyClass ()
#property (nonatomic, strong) NSMutableArray * array;
#end
#implementation MyClass
- (id)init {
self = [super init];
if ( self ) {
_array = [NSMutableArray array];
}
return self;
}
- (IBAction)onButtonClick {
NSString *strr = [textf stringValue];
NSString *strr1 = [textf1 stringValue];
[self.array addObject:strr];
[self.array addObject:strr1];
for ( int i = 0; i < [myArray count]; i++ ) {
NSString * element = [myArray objectAtIndex:i];
NSLog(#"The element at index %d in the array is: %#", i, element);
}
}
#end

Filter NSArray of Object with NSArray of NSNumber with NSPredicate

i'm trying to filer an array of this Object:
MyObject.h
#interface MyObject : NSObject
#property (assign) int id_object;
#property (nonatomic, strong) NSString *name;
#end
So for example in my firstArray i have some MyObject:
MyObject *ob1 = [[MyObject alloc] init];
[ob1 setId_Object:1;
[ob1 setName:#"Carl"];
MyObject *ob2 = [[MyObject alloc] init];
[ob2 setId_Object:2;
[ob2 setName:#"Carl"];
MyObject *ob3 = [[MyObject alloc] init];
[ob3 setId_Object:3;
[ob3 setName:#"Carl"];
NSArray *firstArray = [[NSArray alloc] initWithObjects:ob1,ob2,ob3,nil];
then i want filter this array with NSPredicate with an array of NSNumber, like this:
NSArray *secondArray = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:2],nil];
i have tried this:
NSArray *filteredObject = [firstArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"id_object IN %#",secondArray]];
but the filteredObject is empty, how i can do?
Seems to be your test case that is wrong. I don't think your setId_object does what you think it does when calling it with an NSNumber;
[ob1 setId_object:[NSNumber numberWithInt:1]];
NSLog(#"%d", ob1.id_object);
> 391
[ob1 setId_object:1];
NSLog(#"%d", ob1.id_object);
> 1
Replacing the initializations makes your test case run ok.
EDIT: My test case, cut'n'pasted;
MyObject *ob1 = [[MyObject alloc] init]; ob1.id_object = 1; [ob1 setName:#"Carl"];
MyObject *ob2 = [[MyObject alloc] init]; ob2.id_object = 2; [ob2 setName:#"Carl"];
MyObject *ob3 = [[MyObject alloc] init]; ob3.id_object = 3; [ob3 setName:#"Carl"];
NSArray *firstArray = [[NSArray alloc] initWithObjects:ob1,ob2,ob3,nil];
NSArray *secondArray = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:2],nil];
NSArray *filteredObject = [firstArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"id_object IN %#",secondArray]];
NSLog(#"%#", filteredObject);
>>> <MyObject: 0x100110350> {1,"Carl"}
>>> <MyObject: 0x1001104b0> {2,"Carl"}
once check this one,
NSArray *filteredObject = [questionSections filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"self.Carl contains %# OR self.Carl contains %# ",#"1",#"2"]];
EX:-
NSArray* questionSections=[[NSArray alloc] initWithObjects:[[NSDictionary alloc] initWithObjectsAndKeys:
#"List 3 rooms in your house?",#"name",#"301q.png",#"questionpicture",nil],[[NSDictionary alloc] initWithObjectsAndKeys:#"Name a commonbird?",#"name",#"202q.png",#"questionpicture",nil], nil];
NSArray *filteredObject = [questionSections filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"self.questionpicture contains %# OR self.questionpicture contains %# ",#"2",#"3"]];
NSLog(#"%#",filteredObject);
O/P:--
(
{
name = "List 3 rooms in your house?";
questionpicture = "301q.png";
},
{
name = "Name a common bird?";
questionpicture = "202q.png";
}
)
try replace
#property (nonatomic, copy) NSNumber* id_object;
#property (nonatomic, copy) NSString *name;
instead of
#property (assign) int id_object;
#property (nonatomic, strong) NSString *name;

First try at sorting an array

I am trying to sort an array by the highest number that I assigned to each entry in the array.
However i don't think it's doing anything.
Any suggestions on where the error may be?
Thanks!
- (NSArray*)sortByPercentage:(NSArray*)array {
NSArray *inputArray = array;
NSSortDescriptor *sortDescriptor = [[ NSSortDescriptor alloc] initWithKey:#"percentMatched" ascending:YES];
//nov 8
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *finalResult = [inputArray sortedArrayUsingDescriptors:sortDescriptors];
[sortDescriptor release];
return [NSArray arrayWithArray:finalResult];
}
I'm intrigued by why your sort doesn't work. This does:
#import <Foundation/Foundation.h>
#interface FooObject : NSObject
#property (nonatomic, assign) NSInteger value;
#property (readonly) NSInteger percentMatched;
#end
#implementation FooObject
#synthesize value;
// compute percentMatched as an elementary function of value
- (NSInteger)percentMatched {
return value * 2;
}
#end
int main(int argc, char *argv[]) {
NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init];
FooObject *foo1 = [[FooObject alloc] init];
foo1.value = 50;
FooObject *foo2 = [[FooObject alloc] init];
foo2.value = 5;
FooObject *foo3 = [[FooObject alloc] init];
foo3.value = 10;
NSArray *myFoos = [NSArray arrayWithObjects:foo1,foo2,foo3,nil];
NSArray *sorters = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:#"percentMatched" ascending:YES]];
NSArray *mySortedFoos = [myFoos sortedArrayUsingDescriptors:sorters];
for(FooObject *foo in mySortedFoos ) {
printf("%ld ",foo.percentMatched);
}
}
Prints 10 20 100 to the console as expected.

how to get different array on different index of single array

i have one array named invoiceInfo1 & i pass it to another array named allInfo.
But I want for different index of invoiceInfo ,the different array is created & pass it to allInfo.
Mycode is as follow:
for (int i=0; i<[userdata count]; i++) {
NSLog(#" userdata count :%d",[userdata count]);
invoiceInfo1 = [NSMutableArray arrayWithObjects:[[userdata objectAtIndex:i]valueForKey:#"fname"], [[userdata
objectAtIndex:i]valueForKey:#"name"],
[[userdata objectAtIndex:i]valueForKey:#"address"],
[[userdata objectAtIndex:i]valueForKey:#"city"], nil];
NSLog(#" info1 is:%#",invoiceInfo1); // invoiceInfo get overwrite when loop execute
NSMutableArray* allInfo = [NSMutableArray arrayWithObjects:headers,invoiceInfo1 , nil];
// HERE I Want Generate New Array of different index of invoiceInfo1 & pass it to allInfo
}
Create a NSObject class with header and info array:
#interface AllInfo: NSObject
{
NSMutableArray *header;
NSMutableArray *info;
}
#property (nonatomic, retain) NSMutableArray *header;
#property (nonatomic, retain) NSMutableArray *info;
#end
#implementation AllInfo
#synthesize header;
#synthesize info;
#end
Then implement this code:
allInfo = [[NSMutableArray alloc] init];
for (int i=0; i<[userdata count]; i++) {
NSLog(#" userdata count :%d",[userdata count]);
NSMutableArray *invoiceInfo1 = [[NSMutableArray alloc] init];
[invoiceInfo1 addObject:[userdata objectAtIndex:i]valueForKey:#"fname"]];
[invoiceInfo1 addObject:[userdata objectAtIndex:i]valueForKey:#"name"]];
[invoiceInfo1 addObject:[userdata objectAtIndex:i]valueForKey:#"address"]];
[invoiceInfo1 addObject:[userdata objectAtIndex:i]valueForKey:#"city"]];
NSLog(#" info1 is:%#",invoiceInfo1); // invoiceInfo get overwrite when loop execute
AllInfo *newInfo = [[AllInfo alloc] init];
newInfo.header = headers;
newInfo.info = invoiceInfo1;
[allInfo addObject:newInfo];
// HERE I Want Generate New Array of different index of invoiceInfo1 & pass it to allInfo
}
Hope this will help.

ObjectiveC, creating array of classes

I have a class that contains values:
#interface Params : NSObject {
[...]
NSString *fc;
NSString *sp;
NSString *x;
NSString *y;
[...]
#property (nonatomic, assign) NSString *fc;
#property (nonatomic, assign) NSString *sp;
#property (nonatomic, assign) NSString *x;
#property (nonatomic, assign) NSString *y;
[...]
#end
it's possible to create in objective-c an array of classes? like c# / java?
MyClass[] a = new MyClass[20] ???
a[0].fc = "asd" ?
or something equivalent?
thanks,
alberto
While the standard way to do this would be with an NSArray, you can do this with a C-style array as well:
int numObjects = 42;
Params ** params = malloc(sizeof(Param*) * numObjects);
for (int i = 0; i < numObjects; ++i) {
params[i] = [[[Params alloc] init] autorelease];
}
free(params);
However, using an NSArray would be much simpler. :)
You would use a standard NSArray to do this, in my example I have used views, but you can use pointers to any object.
UIView * object = [[UIView alloc] init];
UIView * object2 = [[UIView alloc] init];
UIView * object3 = [[UIView alloc] init];
NSArray * array = [[NSArray alloc] initWithObjects:object,object2,object3,nil];
[object release];[object2 release];[object3 release];//edit
UIView * test = [array objectAtIndex:0];
test.tag = 1337;