刚刚下载了Xcode 7 Beta,enumerate关键字出现了这个错误.
for (index, string) in enumerate(mySwiftStringArray)
{
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我克服这个吗?
而且,似乎count()不再适用于计算长度String.
let stringLength = count(myString)
Run Code Online (Sandbox Code Playgroud)
在上面一行,编译器说:
'count'不可用:访问集合上的'count'属性.
Apple已经发布了Swift 2.0的任何编程指南吗?
我有以下问题.我使用下面的代码,我得到了问题
"变量'特征'从未发生变异;考虑改为'让'常数"
for var characteristic:CBCharacteristic in service.characteristics ?? [] {
print(str)
_selectedPeripheral!.writeValue(str.dataUsingEncoding(NSUTF8StringEncoding)!, forCharacteristic: characteristic, type: CBCharacteristicWriteType.WithoutResponse)
}
Run Code Online (Sandbox Code Playgroud)
当我改为"let"时,出现错误:
'let'模式不能嵌套在已经不可变的上下文中
为什么它会向我推荐更改,然后将其标记为错误?
我想在Swift中找到第一个带有"单"线表达式EKSource的类型EKSourceType.Local.这是我目前拥有的:
let eventSourceForLocal =
eventStore.sources[eventStore.sources.map({ $0.sourceType })
.indexOf(EKSourceType.Local)!]
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法(例如没有映射和/或通用版本find)?
这是在Playground完成的,只是为了简化.
class MyPrivateVar
{
private var priv: String?
}
var myInstance = MyPrivateVar()
myInstance.priv = "Something"
Run Code Online (Sandbox Code Playgroud)
没有编译器警告.事实上,自动完成显示priv没有问题.我的理解是,在班级{}的范围之外,我不应该看到任何私人的东西,func也不应该var.
我错过了什么吗?
我在Swift中有一个自定义的OptionSetType结构.如何枚举实例的所有值?
这是我的OptionSetType:
struct WeekdaySet: OptionSetType {
let rawValue: UInt8
init(rawValue: UInt8) {
self.rawValue = rawValue
}
static let Sunday = WeekdaySet(rawValue: 1 << 0)
static let Monday = WeekdaySet(rawValue: 1 << 1)
static let Tuesday = WeekdaySet(rawValue: 1 << 2)
static let Wednesday = WeekdaySet(rawValue: 1 << 3)
static let Thursday = WeekdaySet(rawValue: 1 << 4)
static let Friday = WeekdaySet(rawValue: 1 << 5)
static let Saturday = WeekdaySet(rawValue: 1 << 6)
}
Run Code Online (Sandbox Code Playgroud)
我想这样的事情:
let weekdays: WeekdaySet = [.Monday, .Tuesday] …Run Code Online (Sandbox Code Playgroud) #available 在区分watchOS和iOS时似乎不起作用.
以下是iOS和watchOS之间共享的代码示例:
lazy var session: WCSession = {
let session = WCSession.defaultSession()
session.delegate = self
return session
}()
Run Code Online (Sandbox Code Playgroud)
...
if #available(iOS 9.0, *) {
guard session.paired else { throw WatchBridgeError.NotPaired } // paired is not available
guard session.watchAppInstalled else { throw WatchBridgeError.NoWatchApp } // watchAppInstalled is not available
}
guard session.reachable else { throw WatchBridgeError.NoConnection }
Run Code Online (Sandbox Code Playgroud)
似乎它只是默认为WatchOS,并且#available编译器不会考虑它.
我是否滥用此API或是否有其他方法可以区分iOS和WatchOS之间的代码?
更新:似乎我误用了BPCorp提到的API
使用Tali的上述代码解决方案:
#if os(iOS)
guard session.paired else { throw WatchBridgeError.NotPaired }
guard session.watchAppInstalled else { throw WatchBridgeError.NoWatchApp }
#endif …Run Code Online (Sandbox Code Playgroud) 最近我看到了Apple的2015年WWDC主题演讲.我也查看了一些文档,但是我找不到"if if pattern"部分,它是如何写在他们展示的幻灯片上的.(来自Apple Events的 68分00秒视频)
你知道这是指什么吗?还是语法?
我正在寻找一些关于如何使用Swift2for 在圆形边缘周围绘制简单单线串的最新帮助/提示iOS9.我看到涉及旧ObjC片段的相当陈旧的例子,并且仅限于此OS X.这在自定义UIView子类的drawRect()方法中是否可以在iOS中实现?
我正在尝试将Objective-C示例转换为Swift 2,但我遇到了一个小问题.最初的Objective-C片段:
NSMutableArray *inputsOutputs = [NSMutableArray array];
...
[inputsOutputs addObject:@{@"input" : input, @"output" : trackOutput}];
Run Code Online (Sandbox Code Playgroud)
我认为Swift代码应该是:
var inputsOutputs = [Any?]()
...
inputsOutputs.append([ "input": input, "output": trackOutput ])
Run Code Online (Sandbox Code Playgroud)
产生的错误是:
Contextual type 'AnyObject' cannot be used with dictionary literal?
Run Code Online (Sandbox Code Playgroud)
在这种情况下,如何将Objective-C转换为Swift?
原始目标-C:https://developer.apple.com/library/mac/samplecode/avsubtitleswriterOSX/Listings/avsubtitleswriter_main_m.html
我有以下代码:
func registerNotification(name:String, selector:Selector)
{
NSNotificationCenter.defaultCenter().addObserver(self, selector: selector, name: name, object: nil)
}
func registerKeyboardNotifications()
{
let isInPopover = navigationController?.popoverPresentationController != nil
let ignore = isInPopover && DEVICE_IS_IPAD
if !ignore {
registerNotification(UIKeyboardWillShowNotification, selector: Selector("keyboardWillShow:"))
registerNotification(UIKeyboardWillHideNotification, selector: Selector("keyboardWillHide:"))
}
}
Run Code Online (Sandbox Code Playgroud)
在延伸UIViewController.许多viewcontroller重用此代码来注册键盘通知.但是使用Swift 2.2会产生警告.我喜欢新的#selector语法,但不知道在这种情况下如何实现它.
我认为正确的解决方案是制定协议并UIViewController仅针对符合该协议的实例进行扩展.我的代码到目前为止:
@objc protocol KeyboardNotificationDelegate
{
func keyboardWillShow(notification: NSNotification)
func keyboardWillHide(notification: NSNotification)
}
extension UIViewController where Self: KeyboardNotificationDelegate
{
func registerKeyboardNotifications()
{
let isInPopover = navigationController?.popoverPresentationController != nil
let ignore = isInPopover && DEVICE_IS_IPAD …Run Code Online (Sandbox Code Playgroud)