extension of my class override readonly method

My objective-c class has public readonly properties that in my extension I want to initalize as I slowly move my code over to Swift. In Objective-C - I mark the properties as readonly in the header and then mark them again as readwrite in the implementation file.


So…with a Swift extension of this class, how do I do that?

Having trouble parsing; sounds like you just want a private setter or to use constants.

Yes, but in ObjC you don't have to write them manually and I don't want to have to do so in Swift either.


Here is what I mean:


Test.h

@interface Test : NSObject
@property (nonatomic, readonly) BOOL someTest;
@end


Test.m

@interface Test ()
@property (nonatomic, assign) BOOL someTest;
@end


This is a common way of doing it in Objective-C.


It allows you to publicly declare something as readonly - but the compiler creates a private setter within the class for me.


So in the above situation, I now add a Swift extension so I can slowly move my code over to Swift.


Test.swift

extension Test {
     func something() {
          //no way to assign a value to someTest
     }
}


If its not possible, its not possible. Fine, but I want to ask.

I don't know Objective-C well, but I know what you meant by this even less well. I'd very much like to know it more well:

"you don't have to write them manually"

I don't think it's possible.


Because of the limited ways to import obj-c headers into Swift code, the best case scenario would mean exposing the setter to all of the Swift code if you are building an app (by adding an extra header file to the bridging header which declares just that setter in a category for your class), but in a framework you wouldn't be able to import it without including it in the public headers of the framework which would defeat the whole purpose.

Even if you use Objective-C, the setter of someTest is not visible from outside of Test.m .

(Test+Extension.m:)

#import "Test.h"
@implementation Test(Extension)
-(void)someThing
{
    self.someTest = false; /
}
@end


If you can put the extension in the same file as the original class, then you can use private setter as suggested by Jessy.

class Test {
    private(set) var someTest: Bool = false
}
extension Test {
    func someThing() {
        someTest = true
    }
}

I think the issue is that a private setter would only be available to one language, and alex_kac wants to start migrating code and will have a mixed obj-c and swift implementation of the class.


If the property is implemented in Swift with a private setter, it can only be available in whichever file implements the property, and won't be included in the generated header or available to the class's original methods in an obj-c category. Implementing the class in Swift can also have side-effects in obj-c, like requiring that all subclasses be implemented in Swift instead of obj-c.


If it is implemented in objective-C with different versions of the property declaration as originally described, it probably can be made available in other obj-c files with an extra interface declaration of the setter method, but imported obj-c header files can't be limited to a particular Swift file or class (or even made non-public, in a framework target).

extension of my class override readonly method
 
 
Q