Swift and KVC


Marco S Hyman
 

This simple little playground doesn’t print any value for the last line. It is a minimization of an issue I’m having in a larger program where the key-value can not be found. Is it me (likely) or is it swift and KVC?

Xcode 9.3

```
//: Playground - noun: a place where people can play

import Cocoa

var str = "Hello, playground"

class Foo: NSObject {
// ...
}

extension Foo {
var name: String {
return "foo name"
}
}

let foo = Foo()
print(foo.name) // prints “foo name"
let fooName = foo.value(forKey: "name")
print(fooName as! String) // prints nothing
```


Marc


Quincey Morris
 

On Apr 2, 2018, at 19:44 , Marco S Hyman <marc@...> wrote:

Is it me (likely) or is it swift

It’s you, but you can plausibly blame it on Swift.

You need:

extension Foo {
   @objc var name: String {
       return "foo name"
   }
}

(since ‘value(forKey:)’ needs an Obj-C property name).


Marco S Hyman
 

You need:

extension Foo {
@objc var name: String {
return "foo name"
}
}
(since ‘value(forKey:)’ needs an Obj-C property name).
Many, many thanks. One curiosity, though... that works in my application but I still get no output in the Playground sample code.

Marc


Quincey Morris
 

On Apr 2, 2018, at 21:09 , Marco S Hyman <marc@...> wrote:

One curiosity, though... that works in my application but I still get no output in the Playground sample code.

Hmm. I did try it in a playground before I posted, and the output is there. (Xcode 9.3)

Does it persist if you quit and relaunch Xcode?


Marco S Hyman
 

Does it persist if you quit and relaunch Xcode?
Perhaps I’m expecting too much from Xcode. I’d quit and relaunched in order to get output for the code I’d posted. I didn’t expect I’d have to quit and relaunch again because I changed one line. Should have known better.

Yeah, it works.


Quincey Morris
 

On Apr 2, 2018, at 22:39 , Marco S Hyman <marc@...> wrote:

 I didn’t expect I’d have to quit and relaunch again because I changed one line.   Should have known better.

Xcode 9.x has an intermittent bug where it gets confused about what you’ve changed. (It looks like the actual displayed text is inconsistent with some internal memory representation.) It doesn’t happen often, but when it does, the best solution is to close all open source files, and quitting is the quickest way to do that.


Alex Zavatone
 


On Apr 3, 2018, at 12:54 AM, Quincey Morris <quinceymorris@...> wrote:

On Apr 2, 2018, at 22:39 , Marco S Hyman <marc@...> wrote:

 I didn’t expect I’d have to quit and relaunch again because I changed one line.   Should have known better.

Xcode 9.x has an intermittent bug where it gets confused about what you’ve changed. (It looks like the actual displayed text is inconsistent with some internal memory representation.) It doesn’t happen often, but when it does, the best solution is to close all open source files, and quitting is the quickest way to do that.


Apparently Xcode has become Windows 95.  When in doubt, reboot.

: /