non-inherited init


(Daniel Eggert) #1

There're some initializers (namely in NSURLSession) that have a comment:

public /*not inherited*/ init(...

What is this “not inherited” comment trying to convey, and how would one go about implementing this in Swift?

/Daniel


(Philippe Hausler) #2

That is a comment emitted by the importer, unfortunately it is impossible for non objc to mimic that behavior 100% because it is an init method that is constructed from a class method.

@interface Foo : NSObject
+ (Foo *)fooNamed:(NSString *)name; // this is not an inherited init method in that it will always return an instance of a Foo no matter the subclass it is called from
@end

@interface Bar : Foo
@end

// in objc it is like this:
Foo *f = [Bar fooNamed:@“test”];

// in swift it is like this:
let f = Bar(named:”test”)

but obviously by the method fooNamed always returns a Foo instead of an instancetype because it actually looks up Foo objects from a table.

···

On Mar 17, 2016, at 7:35 AM, Daniel Eggert via swift-corelibs-dev <swift-corelibs-dev@swift.org> wrote:

There're some initializers (namely in NSURLSession) that have a comment:

public /*not inherited*/ init(...

What is this “not inherited” comment trying to convey, and how would one go about implementing this in Swift?

/Daniel

_______________________________________________
swift-corelibs-dev mailing list
swift-corelibs-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-corelibs-dev


(Philippe Hausler) #3

Actually partially related. The /*@NSCopying*/ comments are hints that we left when initially importing that the return value should be copied. It is again a export from it’s objc nature. In this case having a property with copy.

e.g.

@property (copy) NSData *data;

exports as

@NSCopying public var data: NSData!

This is mainly there to preserve the copy nature when subclasses are implemented.

···

On Mar 17, 2016, at 7:48 AM, Pushkar N Kulkarni <pushkar.nk@in.ibm.com> wrote:

Sorry for intruding!

I have the same question about /*@NSCopying*/

Pushkar N Kulkarni,
IBM Runtimes

Simplicity is prerequisite for reliability - Edsger W. Dijkstra

-----swift-corelibs-dev-bounces@swift.org <mailto:-----swift-corelibs-dev-bounces@swift.org> wrote: -----
To: Daniel Eggert <danieleggert@me.com <mailto:danieleggert@me.com>>
From: Philippe Hausler via swift-corelibs-dev
Sent by: swift-corelibs-dev-bounces@swift.org <mailto:swift-corelibs-dev-bounces@swift.org>
Date: 03/17/2016 08:12PM
Cc: Swift corelibs dev <swift-corelibs-dev@swift.org <mailto:swift-corelibs-dev@swift.org>>
Subject: Re: [swift-corelibs-dev] non-inherited init

That is a comment emitted by the importer, unfortunately it is impossible for non objc to mimic that behavior 100% because it is an init method that is constructed from a class method.

@interface Foo : NSObject
+ (Foo *)fooNamed:(NSString *)name; // this is not an inherited init method in that it will always return an instance of a Foo no matter the subclass it is called from
@end

@interface Bar : Foo
@end

// in objc it is like this:
Foo *f = [Bar fooNamed:@“test”];

// in swift it is like this:
let f = Bar(named:”test”)

but obviously by the method fooNamed always returns a Foo instead of an instancetype because it actually looks up Foo objects from a table.

> On Mar 17, 2016, at 7:35 AM, Daniel Eggert via swift-corelibs-dev <swift-corelibs-dev@swift.org <mailto:swift-corelibs-dev@swift.org>> wrote:
>
> There're some initializers (namely in NSURLSession) that have a comment:
>
> public /*not inherited*/ init(...
>
> What is this “not inherited” comment trying to convey, and how would one go about implementing this in Swift?
>
> /Daniel
>
> _______________________________________________
> swift-corelibs-dev mailing list
> swift-corelibs-dev@swift.org <mailto:swift-corelibs-dev@swift.org>
> https://lists.swift.org/mailman/listinfo/swift-corelibs-dev

_______________________________________________
swift-corelibs-dev mailing list
swift-corelibs-dev@swift.org <mailto:swift-corelibs-dev@swift.org>
https://lists.swift.org/mailman/listinfo/swift-corelibs-dev


(Pushkar N Kulkarni) #4

Sorry for intruding!

Pushkar N Kulkarni,

IBM Runtimes

Simplicity is prerequisite for reliability - Edsger W. Dijkstra

I have the same question about /@NSCopying/

That is a comment emitted by the importer, unfortunately it is impossible for non objc to mimic that behavior 100% because it is an init method that is constructed from a class method.

@interface Foo : NSObject

  • (Foo *)fooNamed:(NSString *)name; // this is not an inherited init method in that it will always return an instance of a Foo no matter the subclass it is called from
    @end

@interface Bar : Foo
@end

// in objc it is like this:
Foo *f = [Bar fooNamed:@“test”];

// in swift it is like this:
let f = Bar(named:”test”)

but obviously by the method fooNamed always returns a Foo instead of an instancetype because it actually looks up Foo objects from a table.

···

On Mar 17, 2016, at 7:35 AM, Daniel Eggert via swift-corelibs-dev swift-corelibs-dev@swift.org wrote:

There’re some initializers (namely in NSURLSession) that have a comment:

public /not inherited/ init(…

What is this “not inherited” comment trying to convey, and how would one go about implementing this in Swift?

/Daniel


swift-corelibs-dev mailing list
swift-corelibs-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-corelibs-dev


swift-corelibs-dev mailing list
swift-corelibs-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-corelibs-dev

To: Daniel Eggert danieleggert@me.com
From: Philippe Hausler via swift-corelibs-dev
Sent by: swift-corelibs-dev-bounces@swift.org
Date: 03/17/2016 08:12PM
Cc: Swift corelibs dev swift-corelibs-dev@swift.org
Subject: Re: [swift-corelibs-dev] non-inherited init

-----swift-corelibs-dev-bounces@swift.org wrote: -----