computed-properties

Lazy loading variable error

社会主义新天地 提交于 2019-12-02 13:31:54
问题 I am writing a program that involves core data. I created a class variable for my context and entity and have my code written like this: class PersistencyManager { var context : NSManagedObjectContext{ let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate let localContext = appDelegate.managedObjectContext return localContext } var userEntity : NSEntityDescription { let entity = NSEntityDescription.entityForName(EntityNames.User, inManagedObjectContext: context) return

Know which among the dependent keys caused the computed property change

為{幸葍}努か 提交于 2019-12-02 06:20:58
问题 In Ember, is there a way to get which among the dependent keys caused a computed property to recalculate? eg: myProp: Ember.computed('dep1','dep2','dep3', function(){ console.log('MyProp was recalculated due to a change in property :' <dep 1,2 or 3(get the value here)>); return ...; }), Kindly let me know if I should provide any additional details. 回答1: Short answer: No, thats not possible. Long answer: You could save away all dependency keys and compare them on recalculation. 回答2: If you are

How to properly declare a computed property, when calculation uses background threads?

三世轮回 提交于 2019-12-01 12:02:43
问题 I'm trying to declare a computed property that consists of a block, executed in the background thread. So, when I address this property, it's nil, as the computation returns the result when it is not ready. How to better correct this? Thank you! enum Result<T> { case error(error: Error) case success(data: T) } var userID: Result<CKRecordID>? { var result: Result<CKRecordID>? = nil container.fetchUserRecordID { recordID, error in if let error = error { result = .error(error: error) } if let

Watching computed properties

怎甘沉沦 提交于 2019-11-28 21:05:20
I have a component with the following hash { computed: { isUserID: { get: function(){ return this.userId? } } } Should I be watching isUserID or userId for changes? Can you watch computed properties? Saurabh Yes, you can setup watcher on computed property, see the fiddle . Following is the code to set watch on computed property: const demo = new Vue({ el: '#demo', data() { return { age: '' }; }, computed: { doubleAge() { return 2 * this.age; } }, watch: { doubleAge(newValue) { alert(`yes, computed property changed: ${newValue}`); } } }); David William computed: { name: { get: function(){

Are lazy vars in Swift computed more than once?

倾然丶 夕夏残阳落幕 提交于 2019-11-28 08:08:22
Are lazy vars in Swift computed more than once? I was under the impression that they replaced the: if (instanceVariable) { return instanceVariable; } // set up variable that has not been initialized Paradigm from Objective-C (lazy instantiation). Is that what they do? Basically only called once the first time the app asks for the variable, then just returns what was calculated? Or does it get called each time like a normal computed property? The reason I ask is because I basically want a computed property in Swift that can access other instance variables. Say I have a variable called "fullName

Difference between computed property and property set with closure

旧街凉风 提交于 2019-11-27 17:12:27
I'm new to Swift. What is the difference between a computed property and a property set to a closure? I know a computed property gets recalculated each time. Is it different for the closure? i.e. Closure: var pushBehavior: UIPushBehavior = { let lazilyCreatedPush = UIPushBehavior() lazilyCreatedPush.setAngle(50, magnitude: 50) return lazilyCreatedPush }() Computed: var pushBehavior: UIPushBehavior { get{ let lazilyCreatedPush = UIPushBehavior() lazilyCreatedPush.setAngle(50, magnitude: 50) return lazilyCreatedPush } } The first is a stored property that is initialized via a closure. The second

Watching computed properties

蹲街弑〆低调 提交于 2019-11-27 13:26:53
问题 I have a component with the following hash { computed: { isUserID: { get: function(){ return this.userId? } } } Should I be watching isUserID or userId for changes? Can you watch computed properties? 回答1: Yes, you can setup watcher on computed property, see the fiddle. Following is the code to set watch on computed property: const demo = new Vue({ el: '#demo', data() { return { age: '' }; }, computed: { doubleAge() { return 2 * this.age; } }, watch: { doubleAge(newValue) { alert(`yes,

VueJS How can I use computed property with v-for

血红的双手。 提交于 2019-11-27 11:45:50
How can I use computed property in lists. I am using VueJS v2.0.2. Here's the HTML: <div id="el"> <p v-for="item in items"> <span>{{fullName}}</span> </p> </div> Here's the Vue code: var items = [ { id:1, firstname:'John', lastname: 'Doe' }, { id:2, firstname:'Martin', lastname: 'Bust' } ]; var vm = new Vue({ el: '#el', data: { items: items }, computed: { fullName: function(item) { return item.firstname + ' ' + item.lastname; }, }, }); You can't create a computed property for each iteration. Ideally, each of those items would be their own component so each one can have its own fullName

Instance member cannot be used on type

无人久伴 提交于 2019-11-27 04:08:34
I have the following class: class ReportView: NSView { var categoriesPerPage = [[Int]]() var numPages: Int = { return categoriesPerPage.count } } Compilation fails with the message: Instance member 'categoriesPerPage' cannot be used on type 'ReportView' What does this mean? Daniel Krom You just have syntax error when saying = {return self.someValue} . The = isn't needed. Use : var numPages: Int { get{ return categoriesPerPage.count } } if you want get only you can write var numPages: Int { return categoriesPerPage.count } with the first way you can also add observers as set willSet & didSet

Method vs Computed in Vue

限于喜欢 提交于 2019-11-27 02:44:41
What is the main difference between a method and a computed value in Vue.js? They look the same and interchangeable. Computed values and methods are very different in Vue and are definitely not interchangeable in most cases. Computed Property A more appropriate name for a computed value is a computed property . In fact, when the Vue is instantiated, computed properties are converted into a property of the Vue with a getter and sometimes a setter. Basically you can think of a computed value as a derived value that will be automatically updated whenever one of the underlying values used to