computed-properties

What makes a property a computed property in Swift

人盡茶涼 提交于 2019-12-10 10:23:38
问题 Let's started with the code snippet: St Foo { var proA: Int = 0 { // needs initialization willSet { print("about to set proA to \(newValue) from \(proA)") } didSet { print("already set proA to \(proA) from \(oldValue)") } } var ProB: Int { // do not needs initialization return 1 } } let foo = Foo() foo.proA = 23 print(foo.ProB) Here are some of my personal understandings about the the stored and computed property: a: Property with only the observer (willSet and didSet) is not a computed

Why does this really tricky computed property name function works the way it does?

▼魔方 西西 提交于 2019-12-10 09:51:01
问题 @raina77ow recently helped me figure out computed property names. As part of their answer to my question, they shared a really tricky bit of code showcasing interesting aspects of JavaScript: const increment = (() => { let x = 0; return () => ++x })(); const movingTarget = { toString: increment }; const weirdObjectLiteral = { [movingTarget]: 42 }; console.log( weirdObjectLiteral[movingTarget] ); // undefined When I run that sample in the node CLI, that last line continually outputs undefined

KVO on Swift's computed properties

ⅰ亾dé卋堺 提交于 2019-12-09 13:09:22
问题 just wondering if this is possible in Swift 2.2, KVO on a computed property!? ie: var width = 0 var height = 0 private var area : Double { get { return with * height } } self.addOberser(self, forKeyPath: "area", ...... Would a client code modifying the with or height trigger observeValueForKeyPath? Just checking before engaging on a mayor class refactor. KVO's syntax being as annoying as it's is not worth even a playground if someone has an answer at hand (I am assuming answer is NO) regards!

Dynamic/computed keys in JMESPath?

大城市里の小女人 提交于 2019-12-07 01:24:12
问题 From ES2015 with computed properties and Array.reduce/Array.map/Object.assign you can do: [{name: 'foo', age: 43}, {name: 'bar', age: 55}].map( o => ({[o.name]: o.age})).reduce((a, b) => Object.assign(a,b), {}) …and get: { foo: 43, bar: 55 } How do I get this from JMESPath? Attempt: $echo '[{"name": "foo", "age": 43}, {"name": "bar", "age": 55}]' | jp [].{name:age} [ { "name": 43 }, { "name": 55 } ] 回答1: Problem How to construct a Jmespath query that returns objects with arbitrary key-value

Computed (NSObject) Properties in SwiftUI don't update the view

只谈情不闲聊 提交于 2019-12-06 09:24:39
So, I want to have a Text that changes its content based on the contents of my CoreData Model. To do that I used a computed property in Xcode beta 4 but it doesn't seem to work anymore. Either that's a bug or there is some other issue I don't see? The problem I have exactly is that my View (and the computed property) don't seem to get updated when self.objectWillChange.send() is called in my store. I also tried to 'export' my var into the store and get it from there, with the same result... EDIT: I just tried the same with another class and it didn't work with just objectWillChange.send() but

Ember js @each one level deep but I have a two deep level relationship [duplicate]

↘锁芯ラ 提交于 2019-12-05 18:20:42
This question already has an answer here: Ember.js 2.3 implement @each.property observer on a HasMany relationship? 2 answers I have to access a property that is two level's deep on my controller, but the [] only can access one level deep via the emberjs guide. model(params) { params.paramMapping = { page: "page", perPage: "per_page", total_pages: "pages" }; return this.findPaged('contractf', params); }, setupController(controller, model) { model.forEach(function(item) { item.set('sale_price', item.get('dealers_sched_id.sale_price')); }); controller.set('content', model); }, Above, I have my

Cannot assign to property: 'xxxx' is a get-only property

巧了我就是萌 提交于 2019-12-05 08:54:31
问题 I'm using a computed property to get the last book in my books array. However, it seems I can't use this property to directly set a book's position property, as my attempt below shows: struct Book { var position: CGPoint? } class ViewController: UIViewController { var books = [Book]() var currentBook: Book { return books[books.count - 1] } func setup() { // Compiler Error: Cannot assign to property: 'currentBook' is a get-only property currentBook.position = CGPoint.zero } } The following

Dynamic/computed keys in JMESPath?

≯℡__Kan透↙ 提交于 2019-12-05 05:09:13
From ES2015 with computed properties and Array.reduce / Array.map / Object.assign you can do: [{name: 'foo', age: 43}, {name: 'bar', age: 55}].map( o => ({[o.name]: o.age})).reduce((a, b) => Object.assign(a,b), {}) …and get: { foo: 43, bar: 55 } How do I get this from JMESPath? Attempt: $echo '[{"name": "foo", "age": 43}, {"name": "bar", "age": 55}]' | jp [].{name:age} [ { "name": 43 }, { "name": 55 } ] Problem How to construct a Jmespath query that returns objects with arbitrary key-value pairs The keys need to be dynamic, based on the output of a jmespath filter expression Workaround As of

KVO on Swift's computed properties

自古美人都是妖i 提交于 2019-12-03 15:55:22
just wondering if this is possible in Swift 2.2, KVO on a computed property!? ie: var width = 0 var height = 0 private var area : Double { get { return with * height } } self.addOberser(self, forKeyPath: "area", ...... Would a client code modifying the with or height trigger observeValueForKeyPath? Just checking before engaging on a mayor class refactor. KVO's syntax being as annoying as it's is not worth even a playground if someone has an answer at hand (I am assuming answer is NO) regards! ~d That code won't work for two reasons: You must add the dynamic attribute to the area property, as

How to use computed property in a codable struct (swift)

安稳与你 提交于 2019-12-03 12:19:12
I've created a "codable" struct to serialize a data set and encode it to Json. Everything is working great except the computed properties don't show in the json string. How can I include computed properties during the encode phase. Ex: struct SolidObject:Codable{ var height:Double = 0 var width:Double = 0 var length:Double = 0 var volume:Double { get{ return height * width * length } } } var solidObject = SolidObject() solidObject.height = 10.2 solidObject.width = 7.3 solidObject.length = 5.0 let jsonEncoder = JSONEncoder() do { let jsonData = try jsonEncoder.encode(solidObject) let jsonString