我想要列举一些国家,例如:
enum Country: Int {
case Afghanistan
case Albania
case Algeria
case Andorra
//...
}
Run Code Online (Sandbox Code Playgroud)
我选择IntrawValue类型有两个主要原因:
我想确定这个枚举的总数,使用IntrawValue类型简化了这个:
enum Country: Int {
case Afghanistan
// other cases
static let count: Int = {
var max: Int = 0
while let _ = Country(rawValue: max) { max = max + 1 }
return max
}()
}
Run Code Online (Sandbox Code Playgroud)我还需要一个代表一个国家的复杂数据结构,并且有一个数组结构的数组.我可以使用Int-valued枚举轻松地从这个数组下标访问某个国家/地区.
struct CountryData {
var population: Int
var GDP: Float
}
var countries: [CountryData]
print(countries[Country.Afghanistan.rawValue].population)
Run Code Online (Sandbox Code Playgroud)现在,我不知何故需要将某个Country案例转换为String(也就是说
let a …Run Code Online (Sandbox Code Playgroud) 假设有一个属性装饰器,并且一个类在其某些属性上使用该装饰器。
function foo(options?: any) {
return function (target: any, prop: string) {
// some magic
}
}
class Bar {
@foo({ opt1: true }) zoo = 123
}
Run Code Online (Sandbox Code Playgroud)
假设我已经foo在我的单元测试中涵盖了的逻辑,现在我愿意编写一个测试来确保
Bar类foo在其属性上zoo使用了带有选项的装饰器{ opt1: true }
这个测试应该怎么写?
我正在 iOS 上开发一个视频播放器项目。
它使用AVFoundation从视频文件中提取CVPixelBuffer,然后将该缓冲区作为纹理发送到 OpenGL。
概念验证代码的灵感来自Apple 的示例代码。AVFoundation 提供YCbCr 颜色空间中的每一帧,并且需要将其转换为RGB 以在OpenGL 中渲染。根据不同的 YCbCr 标准(例如ITU-R BT.709、ITU-R BT.601),此变换似乎具有多个变换矩阵选项。示例代码通过以下代码确定使用哪一个:
Run Code Online (Sandbox Code Playgroud)CFTypeRef colorAttachments = CVBufferGetAttachment(pixelBuffer, kCVImageBufferYCbCrMatrixKey, NULL); if (colorAttachments == kCVImageBufferYCbCrMatrix_ITU_R_601_4) { _preferredConversion = kColorConversion601; } else { _preferredConversion = kColorConversion709; }
但是,我使用的是 swift 并且返回colorAttachment是类型,Unmanaged<CFTypeRef>而常量kCVImageBufferYCbCrMatrix_ITU_R_601_4是类型,CFString因此它们不能直接相等。我做了一些研究,最后得到了:
CFEqual(colorAttachments, kCVImageBufferYCbCrMatrix_ITU_R_601_4) // returns false
CFEqual(colorAttachments, kCVImageBufferYCbCrMatrix_ITU_R_709_2) // returns false too!!
//-----------------------------------------
CFGetType(colorAttachments) // returns 1
CFStringGetType() // returns 7, note kCVImageBufferYCbCrMatrix_ITU_R_601_4 is …Run Code Online (Sandbox Code Playgroud) 有两个页面(或相对于 vue 术语的组件)都需要相同的数据集,这些数据是通过 http 上的 api 提供的。访问这两个组件的顺序是未定义的(或依赖于用户行为),并且数据应该只获取一次,因为它不会发生很大变化。
我知道 astate存储实际数据,mutations 变异sstate和actions 做异步请求、多变异协调等脏活的想法。
问题是:执行上述缓存逻辑的最佳实践是什么?
我想出了以下三种方法,但对我来说它们都不是完美的:
缺点:
我需要在到处访问数据之前调度操作,因为我不知道数据是否已经被获取。
// ComponentA
async mouted () {
await this.store.dispatch('fetchData')
this.someData = this.store.state.someData
}
// ComponentB
async mouted () {
await this.store.dispatch('fetchData')
this.someData = this.store.state.someData
}
// vuex action
{
async fetchData ({ state, commit }) {
// handles the cache logic here
if (state.someData) return
commit('setData', await apis.fetchData())
}
}
Run Code Online (Sandbox Code Playgroud)缓存逻辑散落在代码库中——臭~
// ComponentA
async mouted () {
if …Run Code Online (Sandbox Code Playgroud)我想实现类似
var a, b, c: MyType = MyType()
Run Code Online (Sandbox Code Playgroud)
但是这行不编译,因为编译器将类型标注MyType仅用于可变c从而a和b缺少任一类型的注释或类型推断一个初始值。
以下两项均合法:
// legal but verbose
var a = MyType()
var b = MyType()
var c = MyType()
// legal but verbose to initialize
var a, b, c: MyType
a = MyType()
b = MyType()
c = MyType()
Run Code Online (Sandbox Code Playgroud)
我可以想到的这两种样式都是合法的,但是有些冗长,尤其是当有许多相同类型的变量时。
有什么优雅的方法可以做到这一点吗?
嗨,我使用下面代码使用的CABasicAnimation更改了帧大小
CABasicAnimation *newanim;
newanim = [CABasicAnimation animationWithKeyPath:@"bounds.size"];
newanim.duration=3.0;
newanim.fromValue=[NSValue valueWithCGSize:CGSizeMake(0,0)];
newanim.toValue=[NSValue valueWithCGSize:CGSizeMake(self.backgroundImageView.bounds.size.width, self.foregroundImageView.bounds.size.height)];
newanim.fillMode=kCAFillModeForwards;
newanim.autoreverses = YES;
newanim.repeatCount = 1;
newanim.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut];
[self.view.layer addAnimation:newanim forKey:@"changeSize"];
Run Code Online (Sandbox Code Playgroud)
但动画从原点开始并向两侧移动(就像动画从中心开始).
我尝试通过改变newanim = [CABasicAnimation animationWithKeyPath:@"frame.size"];
它根本不起作用.
我正在开发一个个人项目(在服务器端非常简单)并计划使用 GraphQL(第一次)。我有点理解 GraphQL 不是 ORM,因此 GraphQL 模式与数据库模式不同。但对于我这个简单的项目来说,服务器端的整体功能只是基本的 CRUD,而且目前可能只有一张表。对于这种情况,我真的不想重复 GraphQL 和一些 ORM 中的模式定义。这两种模式可以统一吗?我应该如何实现这一目标?
PS 我计划使用 NodeJS 进行服务器端开发。
swift ×2
avfoundation ×1
caanimation ×1
cocoa-touch ×1
color-space ×1
database ×1
decorator ×1
enums ×1
graphql ×1
ios ×1
typescript ×1
unit-testing ×1
vue.js ×1
vuex ×1