How do I get out data from my NSDirectory (not just a property typo)? - ios5

I'm missing something simple I think, but been at it for days now without solving this. Even Started to create a "work-around" just to solve it for now, but still want to solve this the "right" way. Any suggestions? Thank's!
.
The problem:
Seems to be missing the class Adealer (get error "-[Adealer objectAtIndex:]: unrecognized selector sent to instance 0x8c5f8b0"), but I did the import Adealer.h to this "detailsVC". But it's not just a simple error of naming the property wrong (objectForKey:#"CustName" instead of "custname" etc - tested this a lot).
Also, I've got similar "listVC"s without a class like Adealer in them, that also transfer data the same way to the same "detailsVC" and they work just fine! Then I just get the data with calls like;
self.labelRestName.text = [restDetails objectForKey:#"CustName"];
Overview:
I got a tableViewController "listVC" that creates the data and show a list, then a ViewController "detailsVC" to show the details. The data (selected row object in "listVC" is transfered via a seque and "destVC.restGPSTransfer" (NSDictionary). The data arrives ok in the "detailsVC" and looks like this in the terminal;
dealerName = Uppsala Centrum Test
dealerAdressStreet = Dragarbrunnsgatan 55
dealerAdressZip = 75320
dealerAdressCity = Uppsala
dealerLongitude = 17.63893
dealerLatitude = 59.85856
dealerDistance2 = 8586398.000000
etc
.
Following the data:
"listVC"
1) First fetching data from web via a AFNetworking json object into an NSMutableArray "restFeed" - ok.
2) Then creating my own data to an NSMutableArray within this loop into a NSMutableArray "updatedDealers" - ok;
NSMutableArray *updatedDealers = [[NSMutableArray alloc]init];
while (i+1 < [_restFeed count]) {
i++;
// Get dealer position function here
// Get distance function here
// Then create my own data here (also #imported Adealer to "listVC";
Adealer *theDealer = [[Adealer alloc]init];
theDealer.dealerName = [[_restFeed objectAtIndex:i]objectForKey:#"CustName"];
theDealer.dealerLongitude = [[_restFeed objectAtIndex:i]objectForKey:#"long"];
theDealer.dealerLatitude = [[_restFeed objectAtIndex:i]objectForKey:#"lat"];
theDealer.dealerDistance2 = theDistance;
// etc...
// Check if data ok
NSLog(#"theDealer = %#",[theDealer description]);
// Don't add dealer object without positiondata to the new array
if (![theDealer.dealerLatitude isEqualToString:#""]) {
[updatedDealers addObject:theDealer];
}
3) Then I use NSSortdescriptor to sort the dealers in NSMutableArray "updatedDealers" into distance order and finally creates the new NSMutableArray "restFeed" with this; (also did "#synthesize dealerFeed = _dealerFeed;" in "listVC") - ok.
_dealerFeed = [NSMutableArray arrayWithArray:sortedContestArray];
4) The populating some tableViewCells with this array and it works just fine - ok.;
cell.cellDealerName.text = [NSString stringWithFormat:#"%#",[[_dealerFeed objectAtIndex:indexPath.row]dealerName]];
5) In the function didSelectRowAtIndexPath transfer the selected object with the "detailsVC"'s NSDictionary "restGPSTransfer" - ok;
destVC.restGPSTransfer = [_dealerFeed objectAtIndex:myIndexPath.row];
"detailsVC":
6) The data seems to transfer ok (se top of this post) but when trying to call the data with;
self.labelRestName.text = [restGPSTransfer objectForKey:#"dealerName"];
I get this error and the app crashes: "-[Adealer objectAtIndex:]: unrecognized selector sent to instance 0x8c5f8b0".
Some more testing done...
Tried to verify the structure + it's keys and properties of the NSDictionary "restGPSTransfer", but using description only got me so far. And have not solved my problem and I still get the "unrecognized selector" error. Could it maybe have become dictionaries within dictionary's or something?
Constructed this little simple if-test to see if the property is really there. But I have to check every property "manually". There's propably a smarter way to check the hole NSDictionary / NSArray?
if ([restGPSTransfer objectForKey:#"dealerName"]) {
NSLog(#"= YES! key exists.");
} else {
NSLog(#"= Nope! key don't exists");
}
THANK'S for any help on this :-)
.
UPDATE the Adealer class files;
Adealer.h
#import <Foundation/Foundation.h>
#interface Adealer : NSObject
#property (nonatomic, retain) NSString * dealerName;
#property (nonatomic, retain) NSString * dealerAdressCity;
#property (nonatomic, retain) NSString * dealerAdressStreet;
#property (nonatomic, retain) NSString * dealerAdressZip;
#property (nonatomic, retain) NSNumber * dealerID;
#property (nonatomic, retain) NSString * dealerImages;
#property (nonatomic, retain) NSString * dealerLogo;
#property (nonatomic, retain) NSString * dealerMail;
#property (nonatomic, retain) NSString * dealerProducts;
#property (nonatomic, retain) NSString * dealerTel;
#property (nonatomic, retain) NSString * dealerText;
#property (nonatomic, retain) NSString * dealerWeb;
#property (nonatomic, retain) NSString * dealerLongitude;
#property (nonatomic, retain) NSString * dealerLatitude;
#property (nonatomic, retain) NSString *dealerDistance;
#property float dealerDistance2;
#end
Adealer.m
#import "Adealer.h"
#implementation Adealer
#synthesize dealerAdressCity, dealerAdressStreet, dealerAdressZip, dealerID, dealerImages, dealerLogo;
#synthesize dealerMail, dealerName, dealerProducts, dealerTel, dealerText, dealerWeb;
#synthesize dealerLongitude, dealerLatitude, dealerDistance,dealerDistance2;
- (NSString *)description {
// Added extension of description
NSMutableString *string = [NSMutableString string];
[string appendString:#"\ntheDealer object and it's properties:\n"];
[string appendFormat:#"dealerName = %#\n", dealerName];
[string appendFormat:#"dealerAdressStreet = %#\n", dealerAdressStreet];
[string appendFormat:#"dealerAdressZip = %#\n", dealerAdressZip];
[string appendFormat:#"dealerAdressCity = %#\n", dealerAdressCity];
[string appendFormat:#"dealerTel = %#\n", dealerTel];
[string appendFormat:#"dealerMail = %#\n", dealerMail];
[string appendFormat:#"dealerWeb = %#\n", dealerWeb];
[string appendFormat:#"dealerLogo = %#\n", dealerLogo];
[string appendFormat:#"dealerImages = %#\n", dealerImages];
[string appendFormat:#"dealerText = %#\n", dealerText];
[string appendFormat:#"dealerProducts = %#\n", dealerProducts];
[string appendFormat:#"dealerLongitude = %#\n", dealerLongitude];
[string appendFormat:#"dealerLatitude = %#\n", dealerLatitude];
[string appendFormat:#"dealerDistance = %#\n", dealerDistance];
[string appendFormat:#"dealerDistance2 = %f\n\n", dealerDistance2];
return string;
}
#end

SOLVED!
Posted if anyone else needs it here.
The solution
In my "detailsVC" I first did this iVar declaration;
.h:
Adealer *theDealer;
#property (nonatomic, retain) Adealer *theDealer;
.m:
#synthesize theDealer;
Then in my "listVC" i did this to transfer the Adealer object and it's properties to the "detailsVC" (remember that the Adealer object already has got it's properties earlier in the described "loop");
Instead of my earlier;
destVC.restGPSTransfer = [_dealerFeed objectAtIndex:myIndexPath.row];
I changed it to;
destVC.theDealer = [_dealerFeed objectAtIndex:myIndexPath.row];
And to actually show and check the transferred property in "detailsVC" I can now simply call this to get the dealers name (or any other properties);
self.labelRestName.text = theDealer.dealerName;
NSLog(#"theDealer.name = %#",theDealer.dealerName);
Works great! Happy Coding everyone!

Related

I get a "Incorrect decrement of the reference count of an object that is not owned at this point by the caller"

When I do Analyze to find out the potential memory leak, I get a "Incorrect decrement of the reference count of an object that is not owned at this point by the caller" :
- (int)downloadUrlTofolder:(NSString *)url filename:(NSString *)name tryTime:(int)tryTime
{
int result = 0;
GetFtpService *ftpService = [[GetFtpService alloc] initwithUrlandOutPut:url output:name];
//I have delete the code here, but problem is not solved.
[ftpService release]; //the potential problem point to this line
return result;
}
Below is the "initwithUrlandOutPut" method:
- (id)initwithUrlandOutPut:(NSString *)url output:(NSString *)o
{
if(self = [super init]) {
self.urlInput = url;
self.outPath = o;
self.success = [NSString stringWithString:#"success"];
self.connected = nil;
}
return self;
}
And the interface:
#interface GetFtpService : NSObject <NSStreamDelegate>
#property (nonatomic, retain) NSInputStream *networkStream;
#property (nonatomic, copy) NSString *urlInput;
#property (nonatomic, retain) NSInputStream *fileStream;
#property (nonatomic, copy) NSString *outPath;
#property int tryTime;
#property (nonatomic, copy) NSString *success;
#property (nonatomic, copy) NSString *connected;
- (id) initwithUrlandOutPut:(NSString *)url output:(NSString *)o;
I want to know why this happened? and how to fix it?
I suspect it's because the 'w' in "initwith..." is not capitalized. Maybe the analyzer is not recognizing the method as an init method because of that.

add data to my data member

Hello I'm new to iPhone development.
I try to add move data from NSDictionary to data member of calls that i created.
When i "setWeightMeasure" nothing happened.
any suggestions?
the code that don't work:
NSDictionary *responseBodyProfile = [responseBody objectFromJSONString];
NSLog(#"%#",responseBodyProfile);
// the output is :
"{ "profile": {"goal_weight_kg": "77.0000", "height_cm": "179.00",
"height_measure": "Cm", "last_weight_date_int": "15452",
"last_weight_kg": "99.0000", "weight_measure": "Kg" }}""
[responseBody release];
if (responseBodyProfile != nil ){
NSDictionary *profile =[responseBodyProfile valueForKey:#"profile"];
NSLog(#"%#\n",[profile objectForKey:#"weight_measure"]);// Output : "kg"
[self.myUser setWeightMeasure:[profile objectForKey:#"weight_measure"]];
NSLog(#"%#", [self.myUser WeightMeasure]); // Output : "(null)"
}
the H file properyty:
#property (nonatomic, retain) UserData* myUser;
UserData.h:
#import <Foundation/Foundation.h>
#interface UserData : NSObject{
NSString* Weight;
NSString* Height;
NSString* GolWeight;
NSString* WeightMeasure;
}
#property (nonatomic, retain) NSString* Weight;
#property (nonatomic, retain) NSString* Height;
#property (nonatomic, retain) NSString* GolWeight;
#property (nonatomic, retain) NSString* WeightMeasure;
#end
UserData.m
#import "UserData.h"
#implementation UserData
#synthesize Weight, Height, GolWeight, WeightMeasure;
-(id)init{
self.Weight = #"0";
self.Height = #"0";
self.GolWeight = #"0";
self.WeightMeasure = #"0";
return self;
}
-(void)dealloc{
[Weight release];
[Height release];
[GolWeight release];
[WeightMeasure release];
[super dealloc];
}
#end
Use valueForKey instead of objectForKey in this line:
[self.myUser setWeightMeasure:[profile objectForKey:#"weight_measure"]];
like this:
[self.myUser setWeightMeasure:[profile valueForKey:#"weight_measure"]];
You might also want to use, since the values could be read as NSNumbers
[self.myUser setWeightMeasure:[[profile valueForKey:#"weight_measure"] stringValue]];
And why do you use strings instead of floats? Wouldn't that make your life easier when you'd need to perform some comparisons?
Also check if you have allocated memory for "myUser", that might be the case as well.
As Eugene mentioned, you should use valueForKey instead of objectForKey
The other thing is you might wanna use property and dot notation whenever you reference your object members, as Apple recommend. It is generally good for you to manage memory.
The previous answer about not initialize your string members in your -init() was totally wrong, if that cause some confusion, I do apologize for it.

Variables emptying or erasing on didSelectRowAtIndexPath

For some reason, I can't access any of my variables after the first IF Statement in the following code. For instance, if index path is [0,0], then the variable phoneText spits out a phone number. But if its [1,0] or [2,0], I get a "null" return. Why is my variable being erased?
The following function in mapviewcontroller.m sets the values. I do actually have an error here that says "instance method setDetails not found".
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
//this determines what kind of item was selected
if ([control isKindOfClass:[UIButton class]]) {
NSLog(#"Trying to load VenueIdentifier...");
FinderAnnotation *clicked = view.annotation;
FinderViewController *fvi = [self.storyboard instantiateViewControllerWithIdentifier:#"FinderDetail"];
NSString* latitude = [NSString stringWithFormat:#"%f",clicked.coordinate.latitude];
NSString* longitude = [NSString stringWithFormat:#"%f",clicked.coordinate.longitude];
NSLog(#"lat: %#",latitude);
NSLog(#"lon: %#",longitude);
[fvi setDetails:clicked.title phone:clicked.phone address:clicked.address beersavailable:clicked.beersavailable latitude:latitude longitude:longitude];
[self.navigationController pushViewController:fvi animated:YES];
}
}
Then my finderdetail.h creates these variables:
#interface FinderDetail : UITableViewController{
UITableViewCell *phone;
UITableViewCell *address;
UITableViewCell *directions;
UILabel *venueLabel;
NSString *phoneText;
NSString *addressText;
NSString *venueText;
NSString *beersavailable;
NSString *latitudeText;
NSString *longitudeText;
}
#property (nonatomic, retain) IBOutlet UITableViewCell *phone;
#property (nonatomic, retain) IBOutlet UITableViewCell *address;
#property (nonatomic, retain) IBOutlet UITableViewCell *directions;
#property (nonatomic, retain) IBOutlet UILabel *venueLabel;
#property (nonatomic, retain) NSString *phoneText;
#property (nonatomic, retain) NSString *addressText;
#property (nonatomic, retain) NSString *venueText;
#property (nonatomic, retain) NSString *beersavailble;
#property (nonatomic, retain) NSString *latitudeText;
#property (nonatomic, retain) NSString *longitudeText;
#end
Lastly, finderdetail.m grabs these values, assigns them to the variables, and spits them into the table:
#implementation FinderDetail
#synthesize venueLabel, phone, address, directions;
#synthesize phoneText, addressText, venueText, beersavailble, latitudeText, longitudeText;
NSString *notlisted;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
-(void)setDetails:(NSString *)v phone:(NSString *)p address:(NSString *)a beersavailable:(NSString *)ba latitude:(NSString *)lat longitude:(NSString *)lon
{
NSLog(#"venue: %#",v);
NSLog(#"phone: %#",p);
NSLog(#"address: %#",a);
NSLog(#"beersavailable: %#",ba);
NSLog(#"%#",lat);
NSLog(#"%#",lon);
latitudeText = lat;
longitudeText = lon;
phoneText = p;
addressText = a;
venueText = v;
beersavailble = ba;
NSLog(#"%#", latitudeText);
NSLog(#"%#", longitudeText);
notlisted = #"Not Listed";
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"Latitude: %#", latitudeText);
NSLog(#"Longitude: %#", longitudeText);
phone.detailTextLabel.text = phoneText;
address.detailTextLabel.text = addressText;
self.venueLabel.text = venueText;
if(phoneText == nil){
phone.detailTextLabel.text = notlisted;
}
if(addressText == nil){
address.detailTextLabel.text = notlisted;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
// Return the number of rows in the section.
if(section ==0)
return 1;
else
if(section ==1)
return 1;
else
if(section ==2)
return 1;
else
return 0;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"%#",indexPath);
if((indexPath.section==0) && (indexPath.row ==0))
{
NSLog(#"%#",phoneText);
}
if((indexPath.section==1) && (indexPath.row ==0))
{
NSLog(#"%#",addressText);
}
if((indexPath.section==2) && (indexPath.row ==0))
{
NSLog(#"%#",latitudeText);
NSLog(#"%#",longitudeText);
}
}
The initial phoneText will display in an NSLog, but the addressText and latitudeText and longitudeText return null. I can put phoneText in one of those lower if statements and it too returns null. Thanks!!!
You aren't actually using your #property when you are doing the following:
latitudeText = lat;
longitudeText = lon;
phoneText = p;
addressText = a;
venueText = v;
beersavailble = ba;
Also, you are leaking memory every time those assignments are performed after the initial time (when they were still nil).
What you really want is:
self.latitudeText = lat;
self.longitudeText = lon;
self.phoneText = p;
self.addressText = a;
self.venueText = v;
self.beersavailble = ba;
Also, with a NSString (also NSData, NSSet, etc.) #property, it is better to define them as a copy, since it would be perfectly valid to pass in a NSMutableString instead (since it is a subclass of NSString), which then the contents could be altered externally of this object:
#property (nonatomic, copy) NSString *phoneText;
#property (nonatomic, copy) NSString *addressText;
#property (nonatomic, copy) NSString *venueText;
#property (nonatomic, copy) NSString *beersavailble;
#property (nonatomic, copy) NSString *latitudeText;
#property (nonatomic, copy) NSString *longitudeText;
Finally, the fact that you get (NULL) outputted by NSLog suggests the ivars are getting set to nil (and most likely released), and you are using ARC (Automatic Reference Counting), instead of manual retain/release/autorelease.
In setDetails you need to use the properties in order to retain the objects and release previous objects. Assigning directly to the ivars subverts the properties setters/getters and the memory management they provide is lost. Basically if properties are defined use them every time.
Since the objects are not being retained their memory can be reused and unpredictable results can occur such as the values becoming nil.
One way to find such problems is to turn on NSZombies in the simulator runs. I do this occasionally even when I am not having problems just as a check.
To fix the problem rewrite setDetails as:
-(void)setDetails:(NSString *)v phone:(NSString *)p address:(NSString *)a beersavailable:(NSString *)ba latitude:(NSString *)lat longitude:(NSString *)lon
{
self.latitudeText = lat;
self.longitudeText = lon;
self.phoneText = p;
self.addressText = a;
self.venueText = v;
self.beersavailble = ba;
self.notlisted = #"Not Listed";
}
One way to insure that properties are not inadvertently not used is to define the ivars with a slightly different name than the properties. The synthesize statement supports this. Here is how:
in the #interface:
NSString *_latitudeText;
...
#property (nonatomic, retain) NSString *latitudeText;
in the #implementation
#synthesize latitudeText = _latitudeText;

objective c interface access

this my first interface in a.h file.
#interface EventRow : NSObject
{
NSString *title;
NSString *photo;
NSString *description;
NSMutableArray *photoArray;
}
#property (nonatomic , retain) NSString *title;
#property (nonatomic , retain) NSString *photo;
#property (nonatomic , retain) NSString *description;
#property (nonatomic , retain) NSMutableArray *photoArray;
#end
in the same file second
#interface PhotoRow : NSObject
{
//NSString *image;
NSMutableArray *imageArray;
}
#property (nonatomic , retain) NSMutableArray *imageArray;
#end
Now every object of "PhotoRow" with filled array(imageArray) stored into the "photoArray" array in EventRow's object.
Now I want to count the total elements of "imageArray" . But getting problem in access it through the EventRow's object.
any suggestions ?
Thanks..
To access the individual objects in photoArray (eventRow is an instance of EventRow
[[eventRow photoArray] objectAtIndex: someIndex]; // I don't like dot notation!
or
[eventRow.photoArray objectAtIndex: someIndex];
To access the imageArray
[[[eventRow photoArray] objectAtIndex: someIndex] imageArray];
or
[eventRow.photoArray objectAtIndex: someIndex].imageArray;
To get the count of images for that imageArray
[[[[eventRow photoArray] objectAtIndex: someIndex] imageArray] count];
or
[eventRow.photoArray objectAtIndex: someIndex].imageArray.count;
If you want to count all of the images, you need loops but you can use fast enumeration to simplifiy things
size_t total = 0;
for (PhotoRow* photoRow in [eventRow photoArray])
{
total += [[photoRow imageArray] count];
}
However, I'd like you to rethink your design a bit. Your exposure of the NSMutableArray in each class breaks encapsulation. Once a caller has got hold of the array, it can modify the internal state of a PhotoRow or an EventRow without the object knowing about it. It would be better not to have the NSMutableArray properties but to add methods to add images and photoRows directly to PhotoRows and EventRows respectively. So, for instance your photoRow class might have the following methods:
-(size_t) imageCount; // returns the result of sending -count to the internal array
-(NSImage*) imageAtIndex: (size_t) index; // returns the result of sending -objectAtIndex: to the underlying array
-(void) addImage: (NSImage*) newImage;
// etc
If I understand you correctly, your photoArray variable in your EventRow class contains PhotoRow objects. You can count the objects in the PhotoRows imageArray variable like so:
int someIndex = 0;
[((PhotoRow*)[myEventRow.photoArray objectAtIndex:someIndex]).imageArray count];

Unknown Memory Leak in iPhone

I am currently building an app for the iPhone and cannot figure out why I keep getting a memory leak to appear in the Leaks Instrument tool.
Here is the code and I have added comments to two places of where it is happening.
NSString *pathname = [[NSBundle mainBundle] pathForResource:self.toUseFile ofType:#"txt" inDirectory:#"/"];
//Line below causes a leak
self.rawCrayons = [[NSString stringWithContentsOfFile:pathname encoding:NSUTF8StringEncoding error:nil] componentsSeparatedByString:#"\n"];
self.sectionArray = [NSMutableArray array];
for (int i = 0; i < 26; i++) [self.sectionArray addObject:[NSMutableArray array]];
for(int i=0; i<self.rawCrayons.count; i++)
{
self.string = [self.rawCrayons objectAtIndex:i];
NSUInteger firstLetter = [ALPHA rangeOfString:[string substringToIndex:1]].location;
if (firstLetter != NSNotFound)
{
NSInteger audio = AUDIONUM(self.string);
NSInteger pictures = PICTURESNUM(self.string);
NSInteger videos = VIDEOSNUM(self.string);
//Line below causes a leak
[[self.sectionArray objectAtIndex:firstLetter] addObject:[[Term alloc] initToCall:NAME(self.string):audio:pictures:videos]];
}
[self.string release];
}
Thanks in advance!
Edit
Here are my property declarations.
#property (nonatomic, retain) NSArray *filteredArray;
#property (nonatomic, retain) NSMutableArray *sectionArray;
#property (nonatomic, retain) UISearchBar *searchBar;
#property (nonatomic, retain) UISearchDisplayController *searchDC;
#property (nonatomic, retain) NSString *toUseFile;
#property (nonatomic, retain) NSArray *rawCrayons;
#property (nonatomic, retain) NSString *string;
#property (nonatomic, retain) TermViewController *childController;
Here are the leaks that are occurring after follow Nick Weaver's fixes.
Here is an expanded version of one of the NSCFString.
And another image.
Image with the Responsible Caller:
Also, because this may be useful, here are the properties for Term:
#property (nonatomic, retain) NSString *name;
#property (nonatomic) NSInteger numberAudio;
#property (nonatomic) NSInteger numberPictures;
#property (nonatomic) NSInteger numberVideos;
And the implementation:
#implementation Term
#synthesize name, numberAudio, numberPictures, numberVideos;
- (Term*)initToCall:(NSString*) toSetName:(NSInteger) audio:(NSInteger) pictures:(NSInteger) videos
{
self.name = [toSetName retain];
self.numberAudio = audio;
self.numberPictures = pictures;
self.numberVideos = videos;
return self;
}
- (NSString*)getName
{
return [[name retain] autorelease];
}
-(void)dealloc
{
[name release];
[super dealloc];
}
#end
Ok, try this changed Version of Temp. I've deleted the getter because you have already one by synthesizing. You cann use the getter like this for name:
term.name
The problem was how you set the name: you want a copy of the name and setting it with the synthesized setter without calling a retain should do the trick. You could, of course, have set it with the retained property of name but you should have left out retain, like this self.name = toSetName;. The setter will retain it for you.
#property (nonatomic, copy) NSString *name;
#property (nonatomic) NSInteger numberAudio;
#property (nonatomic) NSInteger numberPictures;
#property (nonatomic) NSInteger numberVideos;
#implementation Term
#synthesize name, numberAudio, numberPictures, numberVideos;
- (Term*)initToCall:(NSString*) toSetName:(NSInteger) audio:(NSInteger) pictures:(NSInteger) videos
{
self.name = toSetName;
self.numberAudio = audio;
self.numberPictures = pictures;
self.numberVideos = videos;
return self;
}
-(void)dealloc
{
[name release];
[super dealloc];
}
Adding an object to an array will retain the instance, so the retain is 2 because you call
[[Term alloc] initToCall..
Do something like
Term *term = [[Term alloc] initToCall..];
[theArray addObject:term];
[term release];
1. See the arrow in the first line in the address column? Click it!
2. After clicking :)
Hard to tell you why the first one is leaking, because we don't know what the property is declared as. Is it retain? copy? assign? what?
The last one is fairly self explanatory though, you're taking ownership of a Term object, and not releasing it when it's added. addObject: retains its argument, meaning if you don't need that Term anymore, you need to give up ownership. I.e., pass -autorelease to the result of your initToCall:::: (which btw is a very bad name for a method)
Change:
[[self.sectionArray objectAtIndex:firstLetter] addObject:[[Term alloc] initToCall:NAME(self.string):audio:pictures:videos]];
to:
Term *tempTerm = [[Term alloc] initToCall:NAME(self.string):audio:pictures:videos];
[[self.sectionArray objectAtIndex:firstLetter] addObject:tempTerm];
[tempTerm release];
By alloc'ing an object you are responsible for it's release.