how to retain values in the following method? - objective-c

#interface SignDocumentController : UIViewController<NSXMLParserDelegate> {
NSMutableString *signFaxString;
NSString * messageId;
NSMutableData *xmlData;
NSURLConnection *connectionInprogress;
NSURLConnection *connectionInprogress2;
NSString * annotationKey;
NSString *firstName;
NSString *lastName;
NSString *date;
NSString *signature;
IBOutlet UIImageView *image;
}
#property(nonatomic,retain)UIImageView * image;
#end
-(void)parser:(NSXMLParser *)parser
didStartElement:(NSString *) elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqual:#"SignatureInfo"]) {
signFaxString = [[NSMutableString alloc]init];
firstName = [attributeDict objectForKey:#"FirstName"];
lastName = [attributeDict objectForKey:#"LastName"];
date = [attributeDict objectForKey:#"Date"];
signature = [attributeDict objectForKey:#"Signature"];
}
if ([elementName isEqual:#"AddAnnotationResult"]) {
signFaxString = [[NSMutableString alloc]init];
}
}
the values for firstName, lastName, date, signature do not stay and I get an error when I try accessing firstName, lastName ETC in a different method:
[CFString respondsToSelector:]: message sent to deallocated instance 0x4ec63b0
I have tried using :
firstName = [NSString stringWithString attributeDict objectForKey:#"FirstName"];
but that does not work either. I know this is a silly question but I could use some help.
Thanks

you could also declare the firstName and others as property and retain . As below
#property(nonatomic,retain)NSString* firstName;
#property(nonatomic,retain)NSString* lastName;
#property(nonatomic,retain)NSString* date;
#property(nonatomic,retain)NSString* signature;
And in .m class.
#synthesize firstName,date,lastName,signature;
and release them in dealloc function.
Use with self all your property variable in you class.
self.firstName = [NSString stringWithString:attributeDict objectForKey:#"FirstName"];
EDITED:
Also consider #bbum comment ..

To retain it, just send a retain message to the object.
firstName = [[attributeDict objectForKey:#"FirstName"] retain];
release it later.

Related

NSMutableArray loses data

There is lots of help regarding this issue already here but none of the implementations have worked. i.e.: creating (nonatomic, retain) + using self.myArray, Using a dictionary instead of arrays.
What I am doing is parsing information from an xml document, filter out unwanted entries, and try to dynamically store the information for that entry.
When I try to store the info into 2 mutableArrays the information of ONE of them gets lost when trying to access the info outside of my 'parser' method.
Some background code. Not full code. (this also has the dictionary as well)
.h
#interface WikiView : UIViewController {
//scrollview
UIScrollView *ScrollView;
//init
int isIpad;
int orientation;
int parseCount;
//parse data constructs
NSString *subplant;
NSString *element;
NSMutableString *text;
NSString *oldElement;
NSMutableDictionary *dataHolder;
NSMutableArray *dataGroup;
NSMutableArray *dataText;
}
#end
.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
//inits
dataGroup = [[NSMutableArray alloc] init];
dataText = [[NSMutableArray alloc] init];
dataHolder = [[NSMutableDictionary alloc] initWithCapacity:1];
text = [[NSMutableString alloc] init];
//parse the info
[self loadDataFromXML:xmlpath];
//When I call the values for dataText here they are all null
//also when called the objects for dataHolder are null as well
//this outputs the correct array
for (int i = 0; i<[dataGroup count]; i++) {
NSLog(#"%#",dataGroup[i]);
}
//this outputs an array of null objects
for (int i = 0; i<[dataText count]; i++) {
NSLog(#"HI.....%#",dataText[i]);
}
}
//parse function
//method to retrieve data
- (void)loadDataFromXML:(NSString *)xmlpath {
//data is parsed
NSData* data = [NSData dataWithContentsOfFile: xmlpath];
NSXMLParser* parser = [[NSXMLParser alloc] initWithData: data];
[parser setDelegate:self];
[parser parse];
[parser release];
}
//on found characters
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ([subplant isEqualToString:plantid]) {
NSString *s2 = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (![oldElement isEqualToString:element]) {
if (oldElement != nil) {
if (parseCount >= 1) {
//Here I store the values into the proper places
NSLog(#"%#: %#",oldElement,text);
[dataGroup addObject:oldElement];
[dataText addObject:text];
[dataHolder setObject:text forKey:oldElement];
//The values are correct here
}
parseCount++;
}
//if (new tag) reset string
[text setString:s2];
}
else{
//if not new tag append string (takes care of &apos;)
[text appendString:s2];
}
oldElement = element;
}
}
//on did start element
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
//accessing tags of this element
if ([elementName isEqualToString:#"plant"]) {
subplant = [attributeDict valueForKey:#"plant"];
}
element = elementName;
}
-(void)dealloc{
[super dealloc];
[text release]; [dataGroup release]; [dataText release]; [dataHolder release];
}
I create dataGroup and dataText the exact same way but only dataText loses its value.
Any help is appreciated, and if any part of my code is unclear please let me know.
EDIT:
Found the source of the problem.
When I write to the dataText array I rewrite every entry to be the last entry to be entered. In my test case the last entry was the string #"null" creating an array of nulls.
Will be back with solution when found.
EDIT2:
#RuslanSoldatenko Noticed I did not create a new instance of my text string after I set the object in the array. Look at the comments for help.

Create an Array from the xml file

My XML format is like this:
<Rows>
<Row>
<style>String</style?
<thumbnail>String</thumbnail>
</Row>
<Row>
<style>String</style?
<thumbnail>String</thumbnail>
</Row>
</Rows>
How do i create an NSMutableArray like this ({style = "value"; thumbnail = "value";}.....)
Here is my code:
-(void) parser:(NSXMLParser *) parser didStartElement:(NSString *) elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:#"style"]) {
element = [NSString stringWithString:elementName];
if (!soapResults) {
soapResults = [[NSMutableString alloc] init];
}
elementFound = YES;
}
if ([elementName isEqualToString:#"thumbnail1"]) {
element = [NSString stringWithString:elementName];
if (!soapResults) {
soapResults = [[NSMutableString alloc] init];
}
elementFound = YES;
}
}
-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string
{
if (elementFound) {
[soapResults appendString:string];
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI: (NSString*)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:#"style"]) {
[rowDict setValue:soapResults forKey:#"style"];
elementFound = NO;
soapResults = nil;
}
if ([elementName isEqualToString:#"thumbnail1"]) {
[rowDict setValue:soapResults forKey:#"thumbnail1"];
elementFound = NO;
soapResults = nil;
}
}
I'm getting wrong format if I'm doing like this please help me out
Thanks in Advance.
create one object class let say SampleObj
*SampleObj.h*
#interface SampleObj : NSObject
{
NSString *style;
NSString *thumbnail;
}
#property (nonatomic,retain) NSString *style,*thumbnail;
*SampleObj.m*
#implementation SampleObj
#synthesize style,thumbnail;
//dealloc your synthesized value;
do parsing like this.
NSArray *array=[theXML componentsSeparatedByString:#"<Rows>"];
for(int i=1;i<[array count];i++)
{
NSArray *arrayRow=[theXML componentsSeparatedByString:#"<Row>"];
for(int j=1;j<[arrayRow count];j++)
{
SampleObj *custobj = [[SampleObj alloc] init];
NSString *str=[arrayRow objectAtIndex:i];
NSArray *arr1=[str componentsSeparatedByString:#"<style>"];
NSString *data=[arr1 objectAtIndex:1];
NSRange ranfrom=[data rangeOfString:#"</style>"];
custobj.style =[data substringToIndex:ranfrom.location];
arr1=[str componentsSeparatedByString:#"< thumbnail >"];
data=[arr1 objectAtIndex:1];
ranfrom=[data rangeOfString:#"</thumbnail >"];
custobj.thumbnail=[data substringToIndex:ranfrom.location];
[yourarray addObject:custobj];
[custobj release]
}
}
if you want to use the object:
SampleObj *custobj = [yourarray objectAtIndex:rowPosition];
NSLog(#"style : %#", custobj.style);
NSLog(#"thumbnail : %#", custobj.thumbnail);
Hope it will help. sorry for any typo.

iOS - parse xml DATASET

This is my XML data that I need to parse
<GetMessagesResult>
<NewDataSet xmlns="">
<Table>
<Date>21:52:59</Date>
<Message>ABC</Message>
<GroupName>ALL</GroupName>
</Table>
<Table>
<Date>11:23:27</Date>
<Message>DEF</Message>
<GroupName>ALL</GroupName>
</Table>
</NewDataSet>
</GetMessagesResult>
This is my SCMessages.h file
#import <Foundation/Foundation.h>
#interface SCMessages : NSObject
{
NSDate *Date;
NSString *Message;
NSString *GroupName;
}
#property (nonatomic, retain) NSDate *Date;
#property (nonatomic, retain) NSString *Message;
#property (nonatomic, retain) NSString *GroupName;
and this is my SCMessages.m file
#import "SCMessages.h"
#implementation SCMessages
#synthesize Date;
#synthesize Message,GroupName;
- (void)dealloc
{
[super dealloc];
[Date release];
[Message release];
[GroupName release];
}
#end
I used below code to parse the data using NSXMLParser delegate methods
#pragma mark - NSXMLParser Delegate
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName
attributes: (NSDictionary *)attributeDict
{
if( [elementName isEqualToString:#"GetMessagesResult"])
{
if(!soapResults)
soapResults = [[NSMutableString alloc] init];
recordResults = TRUE;
}
if([elementName isEqualToString:#"NewDataSet"]) {
//Initialize the array.
messages = [[NSMutableArray alloc] init];
}
else if([elementName isEqualToString:#"Table"]) {
//Initialize the message.
aMessage = [[SCMessages alloc] init];
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if( recordResults )
[soapResults appendString: string];
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:#"Table"]) {
[messages addObject:aMessage];
NSLog(#"MESSAGES COUNT: %d",messages.count);
NSLog(#"MESSAGE: %#",aMessage);
[aMessage release];
aMessage = nil;
}
// as this is the last element
if( [elementName isEqualToString:#"NewDataSet"])
{
recordResults = FALSE;
}
}
PROBLEM I am not getting the desired message object with Date, Meesage & GroupName.
I put NSLog to print them but I always get null value. The weird thing is message array gets allocated memory & also elements gets added to array as I can see message array count in NSLog but the array element has data as null value.
I am parsing the XML data received in SOAP response in NSURLConnection delegate method connectionDidFinishLoading
NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(#"theXML: \n%#",theXML);
You need to store your values in aMessage. You have added particular object to array but you forgot to store data in that object of SCMessages class.You can also check while debug that you are getting value in aMessage object.
You need to store value like this in this didEndElement method.
[aMessage setValue: soapResults forKey:elementName];
I think this will help you.
Your didEnd method is missing code to store the accumulated string into a property. You need an ivar for the current SCMessage object.
Append text content to a mutable string ivar. Then on closing an element you have to decide which property to set on SCMessage. You probably have to parse the date into an NSDate.

Parse an Xml documents and make an Array with some Xml's Values

First of all, please accept my apologizes for the text. I am french, and it would not be "strange" there are some mistakes in my syntax.
Keep in mind too i am a "newbie" on the objective C world and programming.
So to explain you the problem i am actually in, i need to parse an xml, pick up some informations from it, (put them on an array or a dictionnary) and display them on a view.
this is the kind of Xml i have to parse : XML
So as you can see, you have in this Xml several "echeances" tags. In them you have the previous ten dates and in those dates, you have differents values.
What i have to display at the end ? The tags "Settle" and "Variation" of the nearest date (the first one in the "list") for EACH "echeance" tag. (I also need to pickup the Attribute "nom=" for each "echeance".
Actually i've learned to use the "MVC" coding, and i have some troubles with the "Model" part.
I use Xcode 4.2 and use (i try ...) to use the ARC mode.
So let me see what i have done for the moment
The .h :
#import <Foundation/Foundation.h>
#import "Echeances.h"
#interface XMLToObjectParser : NSObject <NSXMLParserDelegate>
{
NSMutableString *settle;
NSMutableString *settle2;
NSMutableString *change;
NSMutableArray *lstEcheance;
NSString *currentNodeName;
NSMutableString *currentNodeContent;
NSMutableDictionary *dico;
NSMutableArray *settleArray;
NSMutableArray *expiryArray;
NSMutableArray *changeArray;
NSString *expiryName;
int compteur;
Echeance *ech;
}
#property (nonatomic, strong) NSMutableArray *expiryArray;
#property (nonatomic, strong) NSMutableArray *settleArray;
#property (nonatomic, strong) NSMutableArray *changeArray;
#property (nonatomic, strong) NSMutableString *currentNodeContent;
#property (nonatomic, strong) Echeance *ech;
#property (nonatomic, strong) NSString *expiryName;
#property (nonatomic, strong) NSMutableString *settle;
#property (nonatomic, strong) NSMutableString *settle2;
#property (nonatomic, strong) NSMutableString *change;
- (NSMutableArray *)expiry;
- (id)parseXMLAtURL:(NSURL *)url toObject:(NSString *)echeance parseError:(NSError **)error;
#end
The .m :
#import "XMLToObjectParser.h"
#implementation XMLToObjectParser
#synthesize expiryArray;
#synthesize settleArray;
#synthesize changeArray;
#synthesize currentNodeContent;
#synthesize ech;
#synthesize expiryName;
#synthesize settle;
#synthesize settle2;
#synthesize change;
- (NSMutableArray *)expiry
{
return expiryArray;
}
- (NSMutableDictionary *)dico
{
return dico;
}
- (id)parseXMLAtURL:(NSURL *)url toObject:(NSString *)aExpiryName parseError:(NSError **)error
{
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];
[parser parse];
if([parser parserError] && error)
{
*error = [parser parserError];
}
return self;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:#"echeance"])
{
ech.nomEcheance = [attributeDict objectForKey:#"nom"];
[expiryArray addObject:ech.nomEcheance];
NSLog(#"%#", expiryArray);
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:#"settle"] || [elementName isEqualToString:#"variation"])
{
for(int i = 0; i < [expiryArray count]; i++)
{
[settleArray addObject:currentNodeContent];
NSLog(#"settle Array: %#",[settleArray lastObject]);
}
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
[currentNodeContent setString:string];
}
#end
And as you can bet on it ... It does not work.
The thing i really want to do is to make a dictionnary with the attribute "nom=" of "echeance" tag as KEY of my dictionnary and each key associated to the correct settle and variation as explain at the top my topic.
To finish, i would like to thanks all the persons who will try to help me :)
Thanks for reading.
Romain
The principle of NSXMLParserDelegate is the combination of didStartElement(), foundCharacters() and didEndElement():
Echaence-Object has:
nom
settleArray as a mutable array
parseXMLAtUrl:
Create a dictionary object
didStartElement:
Found element 'echeance' => create new object Echeance and store it in Dictionary. Keep pointer to object for further actions in a private property.
found element 'settle' => store flag, we're in settle-mode
foundCharacters:
if in settle_mode => add string to current Echeance-Object's settleArray
didEndElement:
found element 'settle' => delete flag, we're in settle-mode

NSXMLParser does not parse last node (possible memory issue)

I have the following .xml file :
<?xml version="1.0" encoding="UTF-8"?>
<company>
<employee>
<id>0</id>
<firstname>Jack</firstname>
<lastname>Johnson</lastname>
<jobtitle>CEO</jobtitle>
<departmentid>0</departmentid>
<parentid>0</parentid>
</employee>
<employee>
<id>1</id>
<firstname>Mik</firstname>
<lastname>Black</lastname>
<jobtitle>Senior Manager</jobtitle>
<departmentid>0</departmentid>
<parentid>0</parentid>
</employee>
<employee>
<id>2</id>
<firstname>Kim<firstname>
<lastname>Friht</lastname>
<jobtitle>Senior Manager</jobtitle>
<departmentid>0</departmentid>
<parentid>0</parentid>
</employee>
...
The following header file:
#import <Foundation/Foundation.h>
#import "Employee.h"
#interface IdParser : NSObject <NSXMLParserDelegate> {
NSXMLParser *xmlParser;
NSMutableArray *employees;
NSString *currentElement;
Employee *employee;
NSMutableString *tempId, *tempFirstName, *tempLastName, *tempDeptId, *tempJobTitle, *tempParentId;
}
-(NSMutableArray *)getSubordinates:(int)idNumber;
#end
And the following implementation:
#import "IdParser.h"
#import "Employee.h"
#implementation IdParser
- (void)start{
NSString *file = #"/users/localadmin/Desktop/employeeData.xml";
NSFileManager *filemgr = [NSFileManager defaultManager];
NSData *dataBuffer = [filemgr contentsAtPath: file];
xmlParser = [[NSXMLParser alloc] initWithData:dataBuffer];
[xmlParser setDelegate:self];
[xmlParser parse];
}
- (void) parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict{
[currentElement release];
currentElement = [elementName copy];
if([elementName isEqualToString:#"employee"]){
employee = [[Employee alloc]init];
}
if([elementName isEqualToString:#"id"]){
tempId = [[NSMutableString alloc]init ];
}
if([elementName isEqualToString:#"firstname"]){
tempFirstName = [[NSMutableString alloc]init ];
}
if([elementName isEqualToString:#"lastname"]){
tempLastName = [[NSMutableString alloc]init ];
}
if([elementName isEqualToString:#"jobtitle"]){
tempJobTitle = [[NSMutableString alloc]init ];
}
if([elementName isEqualToString:#"departmentid"]){
tempDeptId = [[NSMutableString alloc]init ];
}
if([elementName isEqualToString:#"parentid"]){
tempParentId = [[NSMutableString alloc]init];
}
}
- (void)parser:(NSXMLParser *)parser
foundCharacters:(NSString *)string{
if([currentElement isEqualToString:#"id"]){
[tempId appendString:string];
}
if([currentElement isEqualToString:#"firstname"]){
[tempFirstName appendString:string];
}
if([currentElement isEqualToString:#"lastname"]){
[tempLastName appendString:string];
}
if([currentElement isEqualToString:#"jobtitle"]){
[tempJobTitle appendString:string];
}
if([currentElement isEqualToString:#"departmentid"]){
[tempDeptId appendString:string];
}
if([currentElement isEqualToString:#"parentid"]){
[tempParentId appendString:string];
}
}
-(void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName{
if([elementName isEqualToString:#"employee"]){
[employee setIdNumber:[tempId intValue]];
[tempId release];
[employee setFirstName:tempFirstName];
[tempFirstName release];
[employee setLastName:tempLastName];
[tempLastName release];
[employee setJobTitle:tempJobTitle];
[tempJobTitle release];
[employee setDepartmentIdNumber:[tempDeptId intValue]];
[tempDeptId release];
[employee setParentIdNumber:[tempParentId intValue]];
[tempParentId release]; //IF I REMOVE THIS LINE, THE PROGRAM DOES NOT CRASH
[employees addObject:employee];
[employee release];
}
}
#end
I am experiencing a very strange problem with it. When I am to call the start method implemented in IdParser, it parses everything but when it gets to the last node of the XML (parentid), something strange happens.
The program quits and I receive the following error message:
malloc: * error for object 0x4b33360: incorrect checksum for freed object - object was probably modified after being freed.
* set a breakpoint in malloc_error_break to debug
Current language: auto; currently objective-c
No memory available to program now: unsafe to call malloc
Strangely, when I remove the [tempParentId release]; line, the program runs fine. I have tried rearranging the elements in the XML and the same thing happens again: the program crashes at the last element. It does no make sense to me what is causing the problem as I am new to Objective-C and iOS so I am asking for help. I guess there is a memory problem somewhere because the program runs fine after I remove the line that I mentioned above.
Thanks for any help.
Petar
EDIT:
As I said I am new to Obj-C and I don't understand much about memory management and all the things connected with it so I am using this example to learn and expand my knowledge about it. That said, can you please try to explain what exactly is causing the error described before suggesting how to fix it.
EDIT2:
Sometimes when I run the code, instead of the error message which I described above, the program freezes and in the console I see just:
Current language: auto; currently objective-c
(gdb)
This may be a clue as to what the problem is because I am experiencing random behaviour.
EDIT3:
Employee class:
#import <Foundation/Foundation.h>
#interface Employee : NSObject {
int idNumber;
NSString *firstName;
NSString *lastName;
int departmentIdNumber;
NSString *jobTitle;
int parentIdNumber;
}
-(id)initWithIdNumber:(int)idValue
firstName:(NSString *)firstNameValue
lastName:(NSString *)lastNameValue
departmentIdNumber:(int)departmentIdNumberValue
jobTitle:(NSString *)jobTitleValue
parentIdNumber:(int)parentIdNumberValue;
#property(nonatomic) int idNumber;
#property(nonatomic, retain) NSString *firstName;
#property(nonatomic, retain) NSString *lastName;
#property(nonatomic, retain) NSString *jobTitle;
#property(nonatomic) int departmentIdNumber;
#property(nonatomic) int parentIdNumber;
#end
#import "Employee.h"
#implementation Employee
#synthesize idNumber, firstName, lastName, departmentIdNumber, jobTitle, parentIdNumber;
-(id)initWithIdNumber:(int)idValue
firstName:(NSString *)firstNameValue
lastName:(NSString *)lastNameValue
departmentIdNumber:(int)departmentIdNumberValue
jobTitle:(NSString *)jobTitleValue
parentIdNumber:(int)parentIdNumberValue{
self = [super init];
if(self){
[self setIdNumber:idValue];
[self setFirstName:firstNameValue];
[self setLastName:lastNameValue];
[self setDepartmentIdNumber:departmentIdNumberValue];
[self setJobTitle:jobTitleValue];
[self setParentIdNumber:parentIdNumberValue];
}
return self;
}
-(NSString *) description{
NSString *desc = [[NSString alloc]initWithFormat:#"ID: %d, firstname: %#, lastname: %#, departmentID: %d, jobtitle: %#, parentID: %d", idNumber, firstName, lastName, departmentIdNumber, jobTitle, parentIdNumber];
return desc;
}
#end
I recommend you enable ARC, which will instruct the compiler to write all memory management code for you. As far as I can tell, it does a better job than most experienced Objective-C developers, and certainly better than new ones.
Everything you need to know about ARC is here: http://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/_index.html
You can enable it for an existing project with Edit -> Refactor -> Convert to Objective-C ARC…. Which will modify the code in your project to be ARC compatible (so make sure you create a backup or commit to source control before doing this!).
Beware ARC code will not run on very old versions of iOS. Shouldn't be an issue though.
You are breaking the first rule of ObjC memory management: don't access your ivars directly. Use accessors everywhere but dealloc and init. This will take care of most memory management problems (and solves several other problems to boot). You have a lot of tricky memory management in the above code that would all go away if you used accessors.
#Abhi Beckert's comment about ARC is good, and if you can use ARC you should. IMO, it is the best addition to ObjC many years, and everyone should use it. But even with ARC, use accessors.