Can't release object - objective-c

I can't release this object and I don't know why:
SchedVO *tmpSched;
tmpSched = [SchedVO alloc];
NSString *timeStr;
timeStr = [[node attributeForName:#"timestamp"] stringValue];
tmpSched.date = [NSDate dateWithTimeIntervalSince1970:timeStr.intValue];
tmpSched.uid = [node stringValue];
[playListArr addObject:tmpSched];
[tmpSched release];
sched:
#interface SchedVO : NSObject
{
NSDate *date;
NSString *uid;
}
#property (nonatomic,retain) NSDate *date;
#property (nonatomic,retain) NSString *uid;
#end
I think its somehow the nsdate part. Can someone help me out?

I believe you're missing an init. tmpSched = [[SchedVO alloc] init]; Init increments the Retain count to 1. You can't release something that hasn't been initialized.

Related

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;

Access NSMutableArray from another class [duplicate]

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.

Sort NSArray with custom objects

In my Xcode project I have the following classes:
Address
#interface LDAddress : NSObject{
NSString *street;
NSString *zip;
NSString *city;
float latitude;
float longitude;
}
#property (nonatomic, retain) NSString *street;
#property (nonatomic, retain) NSString *zip;
#property (nonatomic, retain) NSString *city;
#property (readwrite, assign, nonatomic) float latitude;
#property (readwrite, assign, nonatomic) float longitude;
#end
Location
#interface LDLocation : NSObject{
int locationId;
NSString *name;
LDAddress *address;
}
#property (readwrite, assign, nonatomic) int locationId;
#property (nonatomic, retain) LDAddress *address;
#property (nonatomic, retain) NSString *name;
#end
In an subclass of UITableViewController, there is a NSArray containing a lot of unsorted objects of LDLocations. Now, I want to sort the NSArray's objects ascending based on the property city of a LDAddress.
How can I sort the array using NSSortDescriptor?
I tried the following, but the app dumps when sorting the array.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"Address.city" ascending:YES];
[_locations sortedArrayUsingDescriptors:#[sortDescriptor]];
Try making the first key lowercase.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"address.city" ascending:YES];
You can also sort an array with blocks:
NSArray *sortedLocations = [_locations sortedArrayUsingComparator: ^(LDAddress *a1, LDAddress *a2) {
return [a1.city compare:a2.city];
}];
This will allow to sort with multiple types. Like we need to sort the Movie based on time and if the time is equal then need to sort by name.
NSArray *sortedArray = [childrenArray sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
NSNumber *first = [NSNumber numberWithLong:[(Movie*)a timeInMillis]];
NSNumber *second = [NSNumber numberWithLong:[(Movie*)b timeInMillis]];
NSComparisonResult result = [first compare:second];
if(result == NSOrderedSame){
result = [((NSString*)[(Movie*)a name] ) compare:((NSString*)[(Movie*)b name])];
}
return result;
}];
-(NSArray*)sortedWidgetList:(NSArray*)widgetList
{
NSSortDescriptor *firstDescriptor = [[NSSortDescriptor alloc] initWithKey:#"itemNum" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:firstDescriptor, nil];
NSArray *sortedArray = [widgetList sortedArrayUsingDescriptors:sortDescriptors];
return sortedArray;
}

convert NSDictionary's key into an NSNumber

How can i convert the key to an NSNumber? this code gives me the error:
no matter how i try it, i always end up with the error: Incompatible integer to pointer conversion assigning to 'NSNumber' from 'NSIntegar' (aka 'int').
for (id key in consultants)
{
consultantData = [[ConsultantData alloc] init];
consultantData.name = [consultants objectForKey:key];
consultantData.conID = [[NSNumber numberWithInteger:key] integerValue];
NSLog(#"name: %# ID: %#", consultantData.name, consultantData.conID);
[consultantList addObject:consultantData];
[consultantData release];
}
here is my object ConsultantData:
#import <Foundation/Foundation.h>
#interface ConsultantData : NSObject
{
NSString *name;
NSNumber *conID;
}
#property (nonatomic, retain) NSString *name;
#property (nonatomic, retain) NSNumber *conID;
#end
#import "ConsultantData.h"
#implementation ConsultantData
#synthesize name;
#synthesize conID;
-(void) dealloc
{
[name release];
[conID release];
[super dealloc];
}
#end
if i try isMemberOfClass, it never returns true. if i try isKindOfClass, it tells me its a NSString.
consultants is a dictionary i receive from my sever that passes thru an XMLRPC function (server is PHP). from what i've seen, everything is a string in the server dictionary returns.
could be that you have the order wrong.
consultantData.conID = [[NSNumber numberWithInteger:key] integerValue];
means
NSNumber *foo = [NSNumber numberWithInteger:key];
NSInteger bar = [foo integerValue];
consultantData.conID = bar;
you probably want
consultantData.conID = [NSNumber numberWithInteger:[key integerValue]];
which is in verbose form:
NSInteger foo = [key integerValue];
NSNumber *bar = [NSNumber numberWithInteger:foo];
consultantData.conID = bar;
consultantData.conID = [[NSNumber numberWithInteger:key] integerValue];
The above line should be:
consultantData.conID = [NSNumber numberWithInteger:[key integerValue]];
Assuming your key is an NSString, you can't pass the key to [NSNumber numberWithInteger:key] because it is not an integer! Use the intValue method on the key to get an integer, then convert the integer to a NSNumber like so:
consultantData.conID = [NSNumber numberWithInteger:[key intValue]];
integerValue returns an NSInteger, which is not an NSNumber. You should pass the NSNumber directly:
consultantData.conID = [NSNumber numberWithInteger:[key integerValue]];

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;