我写了一个代码块如下:
class Person{
let name:String;
init(name:String){
self.name = name;
println("\(name) is being initialized.");
}
deinit{
println("\(name) is being deInitialized.");
}
}
var person:Person?;
person = Person(name:"leo");
person = nil;
Run Code Online (Sandbox Code Playgroud)
初始化时,没问题print.设置人员时nil,deinit不调用该方法.
我实现了以下类:
class GCDStudy {
func asyncSerial(time:Double){
let queue = dispatch_queue_create("DISPATCH_QUEUE_SERIAL",DISPATCH_QUEUE_SERIAL)
dispatch_async(queue){
var i:Double = 0
while(i < 3){
i++
print("asyncSerial -wait:\(time)-\(i)")
}
}
}
func asyncConcurrent(time:Double){
let queue = dispatch_queue_create("DISPATCH_QUEUE_CONCURRENT",DISPATCH_QUEUE_CONCURRENT)
dispatch_async(queue){
var i:Double = 0
while(i < 3){
i++
print("asyncConcurrent -wait:\(time)-\(i)")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
并运行如下:
运行A:
gCDStudy = GCDStudy()
gCDStudy.asyncSerial(1)
gCDStudy.asyncSerial(2)
Run Code Online (Sandbox Code Playgroud)
跑B
vgCDStudy2 = GCDStudy()
gCDStudy2.asyncConcurrent(1)
gCDStudy2.asyncConcurrent(2)
Run Code Online (Sandbox Code Playgroud)
结果RunA:
asyncSerial -wait:1.0-1.0
asyncSerial -wait:2.0-1.0
asyncSerial -wait:1.0-2.0
asyncSerial -wait:2.0-2.0
asyncSerial -wait:1.0-3.0
asyncSerial -wait:2.0-3.0
Run Code Online (Sandbox Code Playgroud)
RunB的结果:
asyncSerial -wait:1.0-1.0
asyncSerial -wait:2.0-1.0
asyncSerial -wait:1.0-2.0
asyncSerial -wait:2.0-2.0 …Run Code Online (Sandbox Code Playgroud) “ For In”可以遍历数组(值/属性/函数)。
let arr = [1,2,3,4];
arr.__proto__.con = function(){console.log('from array');}
for(let item in arr){
console.log(item);
}
Run Code Online (Sandbox Code Playgroud)
结果将是:
1,2,3,4,con
Run Code Online (Sandbox Code Playgroud)
为什么不打印“ toString” /“ split”之类的本机功能?有什么不同?
在我看来,它们(con和toString)都属于Array的原型。
我使用 AVPlayer 在线播放音频,并希望在 avplayer 完成加载流时将音频数据/流保存到本地。
我按如下方式实现它:
let fileUrl = NSURL(string: strUrl)!
let asset = AVURLAsset(URL: fileUrl)
asset.resourceLoader.setDelegate(self, queue:dispatch_queue_create("AVARLDelegateDemo loader", nil))
self.pendingRequests = [AVAssetResourceLoadingRequest]()
asset.loadValuesAsynchronouslyForKeys(["playable"]){
dispatch_async( dispatch_get_main_queue()){
self.prepareToPlayAsset(asset, requestedKeys: ["playable"])
}
}
func resourceLoader(resourceLoader: AVAssetResourceLoader, shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) -> Bool {
.......
return false
}
Run Code Online (Sandbox Code Playgroud)
当 url 为http/https 时,它不调用resourceLoader(resourceLoader: AVAssetResourceLoader, shouldWaitForLoadingOfRequestedResource .... - 当 url 是自定义的(例如:'test')时,它调用resourceLoader(resourceLoader: AVAssetResourceLoader, shouldWaitForLoadingOfRequestedResource...
谁知道原因,resourceLoader不支持http/https吗?
我有一个本机反应项目,昨天对于android来说还可以,但是今天运行时发生错误react-native run-android。今天我在android上运行之前,什么都没有改变,只能在中打开它AndroidStudio。错误如下:
.gradle / caches / transforms-1 / files-1.1 / appcompat-v7-27.0.2.aar / d1dde4fd0fa50f7f7336597688c557a9 / res / values / value s.xml:252:5-69:AAPT:错误:资源android:attr / fontVariationSettings未找到。
.gradle / caches / transforms-1 / files-1.1 / appcompat-v7-27.0.2.aar / d1dde4fd0fa50f7f7336597688c557a9 / res / values / value s.xml:252:5-69:AAPT:错误:资源android:attr / ttcIndex未找到。
错误:链接引用失败。
失败:构建失败,发生异常。
- 出了什么问题:任务':app:processDebugResources'的执行失败。
无法处理资源,有关详细信息,请参见上面的aapt输出。
并且主要配置如下:
"react": "16.6.3",
"react-native": "0.57.8"
gradle plugin:com.android.tools.build:gradle:3.1.0
compileSdkVersion 27
buildToolsVersion "27.0.3"
minSdkVersion 19
targetSdkVersion 27
Run Code Online (Sandbox Code Playgroud)
我花了很多时间解决它,但失败了。
大多数解决方案是将android升级到AndroidX,但是我不能,因为我的项目有很多依赖项,其中有些不支持AndroidX。
所以我想知道为什么android:attr/ttcIndex我运行它时需要它,以及如何修复它和不要升级Android版本并保持compileSdkVersion 27
Q1:如果在GCD中为队列创建相同的名称,它是否是相同的队列?
class Sample {
private var time:Int64 = 0
func asyncSerial(time:Int64){
let queue = dispatch_queue_create("test",DISPATCH_QUEUE_SERIAL)
dispatch_async(queue){
let delayTime = dispatch_time(DISPATCH_TIME_NOW, time)
self.time = time
print("async:\(self.time)")
dispatch_after(delayTime, queue){
print("wait:\(self.time)")
}
}
print("sync:\(self.time)")
}
}
let s = Sample()
s.result(10)
let s2 = Sample()
s1.result(1)
Run Code Online (Sandbox Code Playgroud)
我在游乐场(Xcode 7.1)运行它,结果如下:
我瘦的结果应该是:
ios ×5
swift ×5
react-native ×2
aapt ×1
android ×1
avasset ×1
avplayer ×1
avurlasset ×1
javascript ×1
reactjs ×1
uitabbaritem ×1
uitabview ×1