1

What I want to do is when user select a district from UITableView in Swift, I'm taking the district name and add .png to it and send the image name variable into Objective-C file and inside it I'm creating UIImageView.

This is my code inside my Objective-C .h file,

@property (weak, nonatomic) NSString *mapImage;

This is how I assign value to it from Swift UITableView when user click on UITableView (didSelectItemAtIndexPath),

selectesState = namesArray[indexPath.row]
let temp :String = selectesState + ".png"
let instanceOfCustomObject: StateMapPinView = StateMapPinView()
instanceOfCustomObject.mapImage = temp

This is the Objective-C code I'm setting the image in .m file (ViewDidLoad),

UIImage *image = [UIImage imageNamed:_mapImage];
_myView = [[VIPhotoView alloc] initWithFrame:self.view.bounds andImage:image];
_myView.autoresizingMask = (1 << 6) -1;

[self.view addSubview:_myView];

But it keeps returning null value.
Whats wrong with my code?

2 Answers 2

2

You have to change the declaration of the property to:

@property (copy, nonatomic) NSString *mapImage;

This will make sure that value is copied when assigned. In your case you use weak and therefore the value is deallocated at the end of the execution of the current scope.

Sign up to request clarification or add additional context in comments.

4 Comments

I changed 'weak' to 'copy' as you suggest but still im getting null.
Can you please post the full code? Especially the part when you read from the mapImage property?
Where exactly do you have nil? In the line where you create image from _mapImage? Have you tried to use self.mapImage? Could you please post the interface and implementation parts of the class where you defined the mapImage property?
StateMapPinView is this the object that holds the table view? Could you please post the source code? And also how you initialise the table view. It looks like instead of updating the object that holds the table view, you create another object and the the mapImage value of it. But this object doesn't have anything to do with the table view holder (I guess the viewDidLoad is called on an object where you initialise the table view).
0

Makesure , you have the same 'instanceOfCustomObject' instance. If it is instantiated again, then you will get null for 'mapImage'.

Use the same instance of 'instanceOfCustomObject' and try in your Obj-c.

CustomObjectClass *instanceOfCustomObject = (CustomObjectClass *) 'same_instanceOfCustomObject';

[UIImage imageNamed: instanceOfCustomObject.mapImage];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.