iOS Interview asked Program set @synthesize for NSString using Non Automatic Reference Counting (ARC) for objective-c

iOS Interview asked Program set @synthesize for NSString using  Non Automatic Reference Counting (ARC) for objective-c ???

First lets do it with old way , without @property and @synthesize. This manually declares the accessors for a property called inventory. Our first iteration of Interview.m provides a straightforward implementation of the getter and setter, along with an instance variable to record the object

We are declaring  String

// Interview.h
#import

@interface Interview: NSObject

- (NSString*)title;
- (void)setTitle:(NSString *)newTitle;

@end


Now implementation

// Interview.h

@implementation
- (void)setTitle:(NSString *)newTitle{
    if (_title== newTitle) {
        return;
    }
    NSNSString *oldValue = _title;
    _title= [newTitle retain];
    [oldValue release];
}

this method make sure! that reference shouldn't be a dangling pointer if the object has been already released earlier in main.m.


// main.m
#import
#import "Interview.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *title= [[NSString alloc] init];
        [title newTitle:@"Honda Civic"];
      
        Interview*superstore = [[Interviewalloc] init];
        [superstore newTitle:@ "inventory"];
        [title release];

        // Do some other stuff...

        // Try to access the property later on (error!) which is removed by above code.
        NSLog(@"%@", [superstore title ]);
    }
    return 0;
}

Now  our answers:-
 There are three property attributes. Each attribute has two or three options, one of which is the default and does not have to explicitly declared.
 you can synthesize a property to generate the code for the accessor methods in the implementation file.
-----------------------------------------------------------------------------------
// Interview.h
#import

@interface Interview: NSObject
@property (nonatomic, readwrite, strong) NSString *itemName;

@end

Created strong object using strong keyword
------------------------------------------------------------------------------------
Implementation
// Interview.h
@implementation
@synthesize itemName;


-----------------------------------------------------------------------------
// main.m
#import
#import "Interview.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *itemName= [[NSString alloc] init];
        self.itemName= @"Hello World";
        [title release];
      }
    return 0;
}









Share on Google Plus

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Post a Comment

Thanks for your Valuable comment