5

I have to translate the following lines of Objective-c code into swift. This is a sample from the Objective-c JSONModel-Framework where the Optional protocol provided by the Framework is applied to an instance variable of type NSString. I found a related post but i didn't managed to achieve it. With my MYModel.swift implementation Xcode complains Cannot specialize non-generic type NSString

thx for your help!

MYModel.swift

@objc(MYModel) public class MYModel : JSONModel {
   ...
   public var name : NSString<Optional>
   ...
}

MYModel.h

@interface MYModel : JSONModel
...
@property (strong, nonatomic) NSString<Optional>* name; 
...

JSONModel.h

...
/**
 * Protocol for defining optional properties in a JSON Model class. Use like below to define 
 * model properties that are not required to have values in the JSON input:
 * 
 * @property (strong, nonatomic) NSString<Optional>* propertyName;
 *
 */
@protocol Optional
@end
...
Community
  • 1
  • 1
robbiebubble
  • 161
  • 9
  • What's the problem you're encountering in translating it? – Antonio Aug 26 '14 at 13:15
  • @Antonio: Updated description – robbiebubble Aug 26 '14 at 13:24
  • You'll probably have problems with that. Swift already has optionals at language level, and non-optional variables must always be initialized during instantiation. But besides that, I presume that the JSON framework you're using use reflection/introspection to populate instances, and that won't work in swift – Antonio Aug 26 '14 at 13:33
  • Thx Antonio. The JSONModel framework is actually working with swift, as long as u use Objective-c types for your swift classes instance variables and annotate the class with @objc(MYModel) – robbiebubble Aug 26 '14 at 13:42
  • Yeah that's right, but making objc compatible automatically means you cannot use swift specific features - such as generics. As long as you are ok with that... – Antonio Aug 26 '14 at 13:49
  • As i couldn't find another framework which is doing proper object-graph-mapping from/to json within swift i'm fine with that. At least for now... :) – robbiebubble Aug 26 '14 at 13:57
  • FYI, I found [this blog post](http://robots.thoughtbot.com/efficient-json-in-swift-with-functional-concepts-and-generics) inspiring - pure swift, a little complex to understand at first. I have used it to develop my own framework, which works fine (unfortunately closed source atm, maybe in a near future I'll be allowed to open it) – Antonio Aug 26 '14 at 14:01

2 Answers2

1

The < and > are not for conforms to protocol. It is for Types with generics like Array:

Array<T>

so you can write var a: Array<String>.

You want something else, a variable should be a Type String and conform to the protocol


You can extend String with the protocol and add the needed functions yourself.

Since your Optional protocol is empty, it is enough to write:

extension NSString: Optional {} // you can use String if you like

To create the protocol write in Swift:

protocol Optional {}

You can Objective-C create the protocol, too.


You should not use Optional, because there is already one, but because Swift has namespacing, it works. You could of course write something like that:

extension NSString: JsonOptProtocol {}

protocol JsonOptProtocol {} // or create that in Objective-C like you did

Documentation link.

Binarian
  • 12,296
  • 8
  • 53
  • 84
  • Thx for your answer! That is actually what i'm looking for. If i try your example `extension NSString: Optional{}` Xcode complains that `Reference to generic type Optional requires arguments in <...>` – robbiebubble Aug 26 '14 at 13:54
  • Haha, oh that is funny, you should not use Optional, because there is already one in the docs. You have to create the protocol of course, added to answer. – Binarian Aug 26 '14 at 14:01
  • It works with Optional, because it knows you are using your self created protocol named `Optional`. But you could also use another name for your protocol of course. – Binarian Aug 26 '14 at 14:02
0

Optional is a type declared in the standard library of Swift, at the moment JSONModel is not compatible with Swift because of this.

Marin Todorov
  • 6,377
  • 9
  • 45
  • 73
  • I see. As a workaround i can overwrite JSONModel's `propertyIsOptional` method to set all parameters "optional" (which is fine for me) as well as the `validate` method to handle possible validation there. – robbiebubble Aug 26 '14 at 14:13
  • yes, I guess a quick fix will be to remove the Optional protocol from JSONModel files, and then just use propertyIsOptional in your models – Marin Todorov Aug 26 '14 at 15:13