1

I created an array called events in Objective-C, and I want to be able to add events to the array in Swift. Here is my declaration of the property in Objective-C:

@property (nonatomic, strong) NSMutableArray *events;

Here is my usage of the array in the Swift file.

var objectC = DPCalendarTestStoryboardViewController()

@IBAction func addMedicine(sender: AnyObject) {
    objectC.events = [].mutableCopy
    var event: DPCalendarEvent = DPCalendarEvent(title: nameText.text, startTime: startDatePicker.date, endTime: startDatePicker.date, colorIndex: 2)
    objectC.events.append(event)
}

Here is the error message:

Value of type 'DPCalendarTestStoryboardViewController' has no member 'events'
1
  • Have you created an objective c bridging header? Commented Apr 9, 2016 at 4:44

1 Answer 1

2

Objective-C NSMutableArray and Swift Array are different. You can still use NSMutableArray and all its methods in Swift.

Replace Code with

@IBAction func addMedicine(sender: AnyObject) {
    objectC.events = NSMutableArray()
    var event: DPCalendarEvent = DPCalendarEvent(title: nameText.text, startTime: startDatePicker.date, endTime: startDatePicker.date, colorIndex: 2)
    objectC.events.addObject(event)
}

and also you need to declare events property in .h file and

instantiate your DPCalendarTestStoryboardViewController viewController using storyboard reference

var objectC =  UIStoryboard(name: "storyboardNameHere", bundle: nil).instantiateViewControllerWithIdentifier("viewControllerIdentifier") as? DPCalendarTestStoryboardViewController
Sign up to request clarification or add additional context in comments.

16 Comments

It still has an error :( I think the problem has something to do more with how I'm calling the array from objectC. But thanks anyways!
You need to declare events property in .h file
Ugh, I got an error. Any idea how to fix it? Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Technovation_2016.CategoryLabelTableViewController 0x7c072510> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key exerciseCell.'
yep, instantiate your DPCalendarTestStoryboardViewController viewController using storyboard reference
Um, sorry, how exactly do you do that?
|

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.