masaldana2 masaldana2 - 1 month ago 7
iOS Question

Objective-c Property returns 'nil' in Swift 3

audio.h

@property(nonatomic,readwrite) int count;


audio.m

_count = 0; count++;


ViewController.swift

var audioSwift: audio?
print(audioSwift?.count)


This returns 'nil' and what I want is to have the modified valued

-Bdridging-Header.h

#import "audio.h"


What to do?
Thank you!

Answer

Don't post answers in comments.

Here is a complete answer:

The problem is that you never create an audio object, so your audioSwift variable is indeed nil.

Change your Swift code like this:

var audioSwift = audio()  //Create a new audio object. 
print(audioSwift.count)

Since the code above creates an audio object right away, it doesn't need to be an Optional any more.

Note that in both Swift and Objective-C class names should start with an upper-case letter, so your audio class should be named Audio instead.