realm-swift icon indicating copy to clipboard operation
realm-swift copied to clipboard

Cannot construct `RLMArray<RLMString>` in Swift

Open SandyChapman opened this issue 7 years ago • 5 comments

I'm not sure if this is a bug or a feature request. It looks like you cannot create an RLMArray of RLMStrings in Swift since Swift requires usage of the RLMArray.init(objectClassName:) initializer which precludes using the RLMPropertyType.string property type.

It seems like its sufficient to simply make the following method public in the RLMArray.h header:

@interface RLMArray (Swift)
// for use only in Swift class definitions
- (instancetype)initWithObjectClassName:(NSString *)objectClassName;
- (instancetype)initWithObjectType:(RLMPropertyType)type optional:(BOOL)optional; // <-- This line is added
@end

Then to construct the array as follows:

@objc public dynamic var myArray = RLMArray<RLMString>(objectType: RLMPropertyType.string, optional: false)

Is this worthwhile to add? Is there any reason why we cannot construct RLMArrays this way?

Thanks.

SandyChapman avatar Feb 23 '18 18:02 SandyChapman

Any response on this?

SandyChapman avatar Mar 26 '18 12:03 SandyChapman

Any response on this?

peterpaulis avatar Oct 31 '19 10:10 peterpaulis

What is the use case for adding this? Seems like a List<String> would suit you, but is there an interop case you can post an example of?

jsflax avatar Oct 31 '19 10:10 jsflax

@jsflax : I don't recall exactly why I was asking for this, but I'd guess it's to allow usages using KVO.

SandyChapman avatar Oct 31 '19 11:10 SandyChapman

I was using the ObjC Realm in Swift and faced issue how to declare an array of String @objc dynamic var paths = RLMArray<RLMString>(objectType: RLMPropertyType.string, optional: false)

turned out

@objc dynamic var paths = RLMArray<RLMString>(objectClassName: String(describing: RLMString.self))

peterpaulis avatar Oct 31 '19 11:10 peterpaulis