关于swift枚举的一般问题.
我想创建一个"icon"的枚举,并将一个值"关联"到枚举的情况
enum Icon {
case plane
case arrow
case logo
case flag
}
Run Code Online (Sandbox Code Playgroud)
我想为枚举值创建一个关联的图像.还有枚举值的相关颜色
因此,例如,如果有可能做类似的事情:
extension Icon.plane {
var image = {
get {
return UIImage("plane.png")
}
}
var color = {
get {
return UIColor.greenColor()
}
}
}
var image = Icon.arrow.image // the image associated to the enum
var color = Icon.arrow.color // the color associated to the enum
Run Code Online (Sandbox Code Playgroud)
这类事可能吗?
enums associated-types computed-values associated-object swift
getter计算属性和返回值的变量之间是否存在差异?例如,以下两个变量之间有区别吗?
var NUMBER_OF_ELEMENTS1: Int {
return sampleArray.count
}
var NUMBER_OF_ELEMENTS2: Int {
get {
return sampleArray.count
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试从服务器获取REST请求返回的对象的简单计数,以便在Ember.js中的另一个控制器中使用
出于这个原因,我需要向服务器发出一个额外的请求.基本上这是我的代码,它几乎可以工作..但还不是.也许有人可以找出原因.
它返回一个PromiseArray,这就是我.then()用来访问属性的原因.
App.TestController = Ember.ObjectController.extend({
totalCount: function() {
return this.store.find('question', {test: this.get('id')}).then(function(items) {
var count = items.get('content').get('length');
console.log(count); // This actually logs correct values
return count;
})
}.property('question')
})
Run Code Online (Sandbox Code Playgroud)
它做了它想做的事情,我在console.log()中打印出正确的值,但是当我尝试{{totalCount}}在视图模板中使用时,我得到的[object Object]不是整数.
另外,我是否正确观察了该questions物业?如果值在适当的控制器中发生变化,值会更新吗?
谢谢