Daniel Fernandez Daniel Fernandez - 1 year ago 77
Objective-C Question

Copying one NSString into other one Xcode

I have a problem in Xcode.I am trying to copy one NSString into other one but is not working.

The original NSString pageTitle is on the RestViewController:
This is my RestViewContoller.h:

#import <UIKit/UIKit.h>
#import "Restaurant.h"

@interface RestViewController : UIViewController

@property (strong, nonatomic) IBOutlet UILabel *TitleLabel;
@property (strong, nonatomic) IBOutlet UILabel *DescriptionLabel;
@property (strong, nonatomic) IBOutlet UIImageView *ImageView;

@property (nonatomic, strong) Restaurant *DetailModal;

@property (nonatomic, strong) NSString *pageTitle;
@property (nonatomic, strong) NSString *pageDescription;


The NSString I want to save the data is on the RestViewController:

@property (nonatomic, strong) Restaurant *DetailModal;

but is a Restaurant class type.

My Restaurant class is:

#import <Foundation/Foundation.h>

@interface Restaurant : NSObject

@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *desc;
@property (nonatomic, copy) NSString *image;

- (instancetype)init:(NSString *)title descripiton:(NSString *)description image:(NSString *)image;



#import "Restaurant.h"

@implementation Restaurant

- (instancetype)init:(NSString *)title descripiton:(NSString *)description image:(NSString *)image {
self = [super init];
if (self != nil) {
self.title = title;
self.desc = description;
self.image = image;
return self;

And finally the place where I want to copy this NSStrings is my RestViewController.m:

#import "RestViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface RestViewController ()


@implementation RestViewController

- (void)viewDidLoad {
[super viewDidLoad];
_pageTitle = @"Test";
_DetailModal.title = _pageTitle;
NSLog(@"_DetailModal.title: %@", _DetailModal.title);


The problem is when I see the result of the

NSLog(@"_DetailModal.title: %@", _DetailModal.title);
on the console,
it puts me:
2016-11-21 11:42:05.407 MadEat[3667:104028]
_DetailModal.title: (null)

What can I do? I know I have a low level of Xcode, but I need your help please. Thank you very much.

Answer Source

Initailize the instance of DataModel First.

Then proceed with assignment

_DetailModal = [[DetailModal alloc] init:_pageTitle descripiton:@"Desc" image:@"image"];

There is a instance method defined already to do the task.

After this also you can alter the value by

_DetailModal.title = @"title_of_yours"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download