Accessing Obj-C properties in Swift extension file

In Swift, private properties are not accessible from another file. This is the meaning of private in Swift. For example:

file1.swift

class MyClass {
    private var privateProperty: String = "Can't get to me from another file!"
}
extension MyClass: CustomStringConvertible {
    var description: String {
        return "I have a `var` that says: \(privateProperty)"
    }
}

file2.swift

extension MyClass {
    func cantGetToPrivateProperties() {
        self.privateProperty // Value of type 'MyClass' has no memeber 'privateProperty'
    }
}

A property declared in the implementation of an Objective-C class is a private property. As such, the property cannot be accessed from a Swift extension since this will be necessarily from a different (.swift) file...


You can access internal objc properties and methods if you declare the objc class extension in a separate header and include that header in the bridging header.

MyClass.h

@interface MyClass : NSObject

@property (nonatomic, copy, readonly) NSString *string;

@end

MyClass+Private.h

#import "MyClass.h"

@interface MyClass ()

@property (nonatomic, copy) NSString *string;

@end

MyClass.m

#import "MyClass+private.h"

@implementation MyClass

//...

@end

Project-Bridging-Header.h

#import "MyClass.h"
#import "MyClass+Private.h"

I think that you can't access private properties from extension. Your scrollView property is in .m file, not .h - which means it's private and it's not visible from extension file.

Solution: move

@property (strong, nonatomic) UIScrollView *scrollView;

to your header file.