100mango / zen Goto Github PK
View Code? Open in Web Editor NEWiOS, macOS, Swift, Objective-C 心得
iOS, macOS, Swift, Objective-C 心得
请问在SwiftTimer被释放的时候,报 EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0),大概会是什么问题?
struct SomeStructure {
static var storedTypeProperty = "Some value."
static var computedTypeProperty: Int {
return 1
}
class var overrideableComputedTypeProperty: Int {
return 107
}
}
这里的SomeStructure是Struct,struct是不能继承的。而且struct只能用static,不能用class。
Crash in Cocoa
Cocoa中会导致Carsh的地方:
crash。。。
__weak SomeObjectClass *weakSelf = self;
SomeBlockType someBlock = ^{
SomeObjectClass *strongSelf = weakSelf;
if (strongSelf == nil) {
// The original self doesn't exist anymore.
// Ignore, notify or otherwise handle this case.
}
[self someMethod];
};
确定是[self someMethod];?
我一直有研究图形性能方面的东西,关于像素重绘方面,我一直有一个解决不了的问题,为了避免一个像素被过度重绘,我们最好是每个视图都加上背景颜色,特别是文本类的,但在iOS8下,UILabel绘制中文的方式跟iOS8以前的并不一样,即使给UILabel对象添加背景色,也会出现像素重绘问题,我解决的方案是,设置UILabel对像关联的layer的masksToBounds为YES,这样确定是可以解决像素重绘问题,但有些文本会莫名的出现多余的横线,是绘制文本的时候出现的,不知道作者对这块是否有了解。
造成
之前对Swift只停留在基本了解,一直在犹豫要不要转Swifter,现在看来,Swift这条船我还是上吧!
很感谢作者的这些Tip!能少走很多弯路!
在 offscreen render 的那个小节里面,分析圆角的时候,offerScreen 是笔误吗?
文章最后给出
let timer = SwiftTimer.repeaticTimer(interval: .seconds(1)) {
SwiftTimer.throttle(interval: .seconds(1.5), identifier: "not pass") {
print("should not pass")
}
}
如果 throttle 的时间大于外层的时间,那么这个 print("should not pass")
永远不会执行,因为在执行 print 之前就会被外层更短的时间 cancel 掉了。
|——|——|
1s 1s
·——x--·
·——x--·
1.5s
那么如何做到文中所说的过滤呢?
我看到有些event loop的实现,是开启一个线程,使用while做一个死循环,在里面接收状态并处理,使用的是<sys/event.h>里面的kevent,比如这个:https://github.com/douban/DOUAudioStreamer/blob/master/src/DOUAudioEventLoop.m,
并没有使用线程自己的RunLoop。
这个需求能通过RunLoop实现吗,这两个是一个东西吗,我有点蒙圈了。求解!
nonatomic,atomic 所有的Swift properties 都是nonatomic。但是我们在线程安全上已经有许多机制,例如NSLock,GCD相关API等。个人推测原因是苹果想把这一个本来就用的很少的特性去掉,线程安全方面交给平时我们用的更多的机制去处理。
苹果官方,给出了一个替代/模拟 atomic 的方案,可以参考下:
let sourceWallTimer = DispatchSource.makeTimerSource(queue: DispatchQueue.global())
let wallTimeInterval = DispatchTimeInterval.seconds(1)
let walltime = DispatchWallTime.now() + wallTimeInterval
print("time-now : \(DispatchWallTime.now())")
print("walltime : \(walltime)")
sourceWallTimer.scheduleRepeating(wallDeadline: walltime, interval: wallTimeInterval)
sourceWallTimer.setEventHandler {
NSLog("定时器运行:--walltime")
}
if #available(iOS 10.0, *) {
sourceWallTimer.activate()
} else {
// Fallback on earlier versions
sourceWallTimer.resume()
}
我运行的结果是这样的,我发现walltime
time-now : DispatchWallTime(rawValue: 16968580581812839616)
walltime : DispatchWallTime(rawValue: 16968580580812844616)
1分钟左右之后的再次运行
time-now : DispatchWallTime(rawValue: 16968580437033322616)
walltime : DispatchWallTime(rawValue: 16968580436033328616)
==》从这个参数上来看walltime可能是从现在到未来的最大值之间的一个 时间长度值
这个值看起来让我觉得并没有设么规律
可能由于模拟器的时间和现实的时间并不一致,大体的时间是不正确的。
但是这个walltime的使用,使得定时器并不能运行,如果使用DispatchTime的初始化时间是没有问题的。
在 viewDidLoad 中创建 timer 并用作为 strong 属性,代码如下:
NSTimer *time = [NSTimer timerWithTimeInterval:3.0 target:self selector:@selector(timeEnd:) userInfo:@"userinfo" repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:time forMode:NSDefaultRunLoopMode];
self.mytimer = time;
运行在 iOS9.2 模拟器上时,dealloc 方法是会调用的,不像你上面所说的强引用问题。
你可以再看看
如题~好像写反了(⊙o⊙)哦
文章写的都不错,对新手很有帮助。能不能分享一些关于网络的知识。比如,哪些通信机制已经被实现了,哪些需要自己写。我猜想与网站服务器,数据库服务器通信的机制应该已经实现了。 这些无需开发者自己再写。 视频传输或其他的,我不是很清楚是否已经实现了。谢谢🙏
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.