Apple的文档指出:
首次初始化属性时,不会调用willSet和didSet观察者.仅在属性的值设置在初始化上下文之外时才调用它们.
是否有可能在初始化期间强制调用它们?
假设我有这门课
class SomeClass {
var someProperty: AnyObject {
didSet {
doStuff()
}
}
init(someProperty: AnyObject) {
self.someProperty = someProperty
doStuff()
}
func doStuff() {
// do stuff now that someProperty is set
}
}
Run Code Online (Sandbox Code Playgroud)
我创建了方法doStuff
,使处理调用更简洁,但我宁愿只处理didSet
函数中的属性.有没有办法在初始化期间强制调用它?
我决定只为我的类删除便利的intializer并强制你在初始化后设置属性.这让我知道didSet
将永远被召唤.我还没有决定这总体上是否更好,但它很适合我的情况.
所以我可以这样做:
var stringNumb: NSString = "1357"
var someNumb: CInt = stringNumb.intValue
Run Code Online (Sandbox Code Playgroud)
但我无法找到方法来做到这一点String
.我想做点什么:
var stringNumb: String = "1357"
var someNumb: Int = Int(stringNumb)
Run Code Online (Sandbox Code Playgroud)
这也不起作用:
var someNumbAlt: Int = myString.integerValue
Run Code Online (Sandbox Code Playgroud) 假设我有这些协议:
protocol SomeProtocol {
}
protocol SomeOtherProtocol {
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我想要一个采用泛型类型的函数,但该类型必须符合SomeProtocol
我可以做的:
func someFunc<T: SomeProtocol>(arg: T) {
// do stuff
}
Run Code Online (Sandbox Code Playgroud)
但有没有办法为多个协议添加类型约束?
func bothFunc<T: SomeProtocol | SomeOtherProtocol>(arg: T) {
}
Run Code Online (Sandbox Code Playgroud)
类似的东西使用逗号,但在这种情况下,它会启动一个不同类型的声明.这是我尝试过的.
<T: SomeProtocol | SomeOtherProtocol>
<T: SomeProtocol , SomeOtherProtocol>
<T: SomeProtocol : SomeOtherProtocol>
Run Code Online (Sandbox Code Playgroud) 我一直在尝试检查CocoaPods的新框架设置,以便让一些Pod继续运行,而我在使用我的Objective-C项目中的Swift问题时遇到了麻烦.
首先,这是CocoaPods预发布0.35,您可以在这里阅读有关如何使用和安装它的信息.
这是我当前的Podfile:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
pod 'MBProgressHUD'
pod 'SLPagingViewSwift'
Run Code Online (Sandbox Code Playgroud)
MBProgressHUD是一个常见的旋转指示器,SLPagingViewSwift是一个随机项目,我通过将Swift键入cocoapods搜索找到.这是ViewController.m
在我的项目中:
#import "ViewController.h"
@import SLPagingViewSwift;
@import MBProgressHUD;
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// Works just fine
MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:hud];
[hud show:YES];
// Causes Error -- Won't build
SLPagingViewSwift *sl = [[SLPagingViewSwift alloc] init];
}
@end
Run Code Online (Sandbox Code Playgroud)
这是SLPagingViewSwift
宣言:
class SLPagingViewSwift: UIViewController, UIScrollViewDelegate {
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,它继承自UIViewController
,因此分配它并初始化它不应该是一个问题.如果我将文件单独添加为文件,上面的代码运行就好了.我知道它有效.
TL;博士
如何在纯Objective-C类中使用CocoaPods创建的纯Swift框架?
故障排除
大多数情况下,我一直在尝试各种进口.Apple推荐这里 …
我似乎无法在文档中找到它,我想知道它是否存在于原生Swift中.例如,我可以NSTimer
像这样调用类级函数:
NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector: "someSelector:", userInfo: "someData", repeats: true)
Run Code Online (Sandbox Code Playgroud)
但我似乎无法找到一种方法来使用我的自定义对象,所以我可以这样称呼它:
MyCustomObject.someClassLevelFunction("someArg")
Run Code Online (Sandbox Code Playgroud)
现在,我知道我们可以将Objective-C与Swift混合使用,并且NSTimer
类方法可能是该互操作性的残余.
Swift中是否存在类级函数?
如果是,我如何在Swift中定义类级别函数?
我有一个数组:
let individualScores = [75, 43, 103, 87, 12]
Run Code Online (Sandbox Code Playgroud)
我这样迭代:
for score in individualScores {
}
Run Code Online (Sandbox Code Playgroud)
但是,有没有办法明确声明对象类型?我认为它会在以后用自定义对象或其他原因派上用场.就像是:
for Integer score in individualScores {
}
Run Code Online (Sandbox Code Playgroud) 如何从类型对象获取Swift类型的值Unmanaged<AnyObject>
.我的例子是使用ABRecordRef
我创建了一个联系对象来管理ABRecordRef
,但是我在从ObjC翻译时遇到了麻烦.这就是我所拥有的:
init(recordRef: ABRecordRef) {
if let firstName = ABRecordCopyValue(recordRef, kABPersonFirstNameProperty) {
self.firstName = firstName
}
}
Run Code Online (Sandbox Code Playgroud)
如果是ObjC,我会这样做:
CFTypeRef firstNameRef = ABRecordCopyValue(recordRef, kABPersonFirstNameProperty);
if (firstNameRef) {
self.firstName = (__bridge NSString *)firstNameRef;
}
Run Code Online (Sandbox Code Playgroud)
我似乎找不到正确的向下转换/转换组合,所以任何帮助都表示赞赏.
我不确定之前是否曾经问过,但我很难找到它.也许我没有使用正确的搜索词,所以如果答案已经存在,如果有人可以指出我正确的方向,那将是非常感谢!
我刚刚注意到锁屏的"滑动解锁"文本中的微光动画随iOS 7.1更新而改变.聚光灯现在有一个卵形/钻石形状,它在字母之间层叠而不会出现在它后面的视图上.
在过去,我通过按顺序改变单个字母的颜色来复制这种类型的特征,但为此,动画通过字母的中间.不影响背景.
我怎么能复制这个?
我找不到我读到的地方,但我记得遇到过一些建议最好使用CGRect
s CGRectGetHeight(rect)
而不是通过访问变量来访问s的高度rect.size.height
CGFloat height = CGRectGetHeight(self.frame);
// vs
CGFloat height = self.frame.size.height;
Run Code Online (Sandbox Code Playgroud)
大多数时候,这与我使用的视图有关,我想知道是否存在区分这两行代码的真正差异(除语法之外).
如果一个优先于另一个,那么解释为什么会很棒!
所以我有一个具有聊天部分的应用程序,我正在同步键盘的动画隐藏和显示聊天输入的上升和下降.
这是我正在使用的代码:
节目:
- (void) keyboardWillShow:(NSNotification *)note {
NSDictionary *keyboardAnimationDetail = [note userInfo];
UIViewAnimationCurve animationCurve = [keyboardAnimationDetail[UIKeyboardAnimationCurveUserInfoKey] integerValue];
CGFloat duration = [keyboardAnimationDetail[UIKeyboardAnimationDurationUserInfoKey] floatValue];
NSValue* keyboardFrameBegin = [keyboardAnimationDetail valueForKey:UIKeyboardFrameBeginUserInfoKey];
CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];
// working for hardware keyboard
//UIViewAnimationOptions options = (UIViewAnimationOptions)animationCurve;
// working for virtual keyboard
UIViewAnimationOptions options = (animationCurve << 16);
[UIView animateWithDuration:duration delay:0.0 options:options animations:^{
textView.frame = CGRectMake(0, self.view.bounds.size.height - keyboardFrameBeginRect.size.height, self.view.bounds.size.width, -40);
} completion:nil];
}
Run Code Online (Sandbox Code Playgroud)
隐藏:
- (void) keyboardWillHide:(NSNotification *)note {
NSDictionary *keyboardAnimationDetail = [note …
Run Code Online (Sandbox Code Playgroud) swift ×7
ios ×4
objective-c ×4
animation ×2
abrecordref ×1
cgrect ×1
cocoa ×1
cocoapods ×1
didset ×1
ios7.1 ×1
quartz-2d ×1
swift2 ×1
uikeyboard ×1