我有以下代码:
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) 我只是用swift 2.2下载一个新的Xcode(7.3).
它有一个警告:
不推荐使用C-style for语句,将在未来的Swift版本中删除.
我该如何修复此警告?
考虑以下:
struct SomeStruct {}
var foo: Any!
let bar: SomeStruct = SomeStruct()
foo = bar // Compiles as expected
var fooArray: [Any] = []
let barArray: [SomeStruct] = []
fooArray = barArray // Does not compile; Cannot assign value of type '[SomeStruct]' to type '[Any]'
Run Code Online (Sandbox Code Playgroud)
我一直试图找到这背后的逻辑,但没有运气.值得一提的是,如果将结构更改为类,则可以完美地运行.
总是可以添加一个变通方法并映射fooArray的每个对象并将它们转换为Any类型,但这不是问题.我正在寻找一个解释为什么这样做的样子.
有人可以解释一下吗?
问题:
使用swift 2.2在Xcode 7.3下运行以下代码时,编译器无法正确推断可选的类型:
import Foundation
func whatAmI<T>(inout property:T?)
{
switch property {
case is Int?:
print("I am an Int?")
case is String?:
print("I am a String?")
default:
print("I don't know what I am")
}
}
var string : String?
whatAmI(&string)
Run Code Online (Sandbox Code Playgroud)
在我身边使用Xcode 7.3,这将打印出来 I am an Int?
但是,当我在将变量传递给函数之前使用空字符串初始化变量时,开关会将其推断为String?.
这将I am a String?
在之前的Xcode版本中打印.
你得到类似的结果吗?
观察:
使用此函数签名时也会出现同样的情况:
func whatAmI(property:AnyObject?)
Run Code Online (Sandbox Code Playgroud)
- 错误 -
这个问题是swift 2.2中的回归:https: //bugs.swift.org/browse/SR-1024
首先非常感谢您的帮助.我昨天刚刚升级了包含swift 2.2的Xcode.我遇到了一些问题但我通过遵循Natashatherobot的"swift 2.2中的新内容"主题来快速修复它们.但有一个问题我无法解决.这是关于UIFont的可用初始化程序,它是在swift 2.2中引入的.附件是一段简单的代码,它将在swift 2.2中报告错误.在我清理项目之前,它可能不会立即报告错误.
lazy var somelabel: UILabel = {
let label = UILabel()
let font = UIFont(name: "somefont", size: 10) ?? UIFont.systemFontOfSize(10) //this line gave me error
label.font = font
label.text = "Calculating..."
return label
}()
Run Code Online (Sandbox Code Playgroud)
错误是:(名称:字符串,大小:CGFloat) - > UIFont'不能转换为'(名称:字符串,大小:CGFloat) - > UIFont?'
我可以通过两种方式解决它:
方法1:不要把这一行
let font = UIFont(name: "somefont", size: 10) ?? UIFont.systemFontOfSize(10)
放在'lazy instantiation'闭包中.(把它放在计算属性报告中没有错误)
方法2:而不是使用:
UIFont(name: "somefont", size: 10)
Run Code Online (Sandbox Code Playgroud)
使用下面的代替(但我不认为这应该是正确的方法,因为它使初始化器更"objc"样式):
UIFont.init(name: "somefont", size: 10)
Run Code Online (Sandbox Code Playgroud)
但我仍然不明白为什么它会在lazy属性关闭中报告错误.如果有人能给我一些解释,我将非常感激.
我对swift中的TextFiled有疑问
我如何在单击它时清除TextFiled中的文本
我的textfiled.text ="0"
我想当我点击textfiled删除数字"0"automatica
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet var lbltext: UILabel!
@IBOutlet var scrolview1: UIScrollView!
@IBOutlet var fi: UITextField!
@IBOutlet var scrolviewus: UIScrollView!
@IBOutlet var counterLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
fi.text = "0"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func button(sender: AnyObject) {
lbltext.numberOfLines = 0
lbltext.text! = lbltext.text! + "\n" + fi.text! + "\n" + "--- "
}
}
Run Code Online (Sandbox Code Playgroud) 我正在将地图应用于包含其中的字典try
.如果映射项无效,我想跳过迭代.
例如:
func doSomething<T: MyType>() -> [T]
dictionaries.map({
try? anotherFunc($0) // Want to keep non-optionals in array, how to skip?
})
}
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,如果anotherFunc
返回nil
,如何转义当前迭代并继续下一步?这样,它就不会包含那些项目nil
.这可能吗?
我试图更深入地了解Swift如何复制值类型:
您在代码中看到的行为将始终如同副本一样.但是,当绝对必要时,Swift仅在幕后执行实际复制.
为了提高我的理解,我想得到一个值类型的内存地址.我尝试了unsafeAddressOf(),但这不适用于结构,它似乎将Swift的标准库类型转换为引用类型(例如,String被转换为NSString).
如何获取值类型的内存地址,如Int的实例,或Swift中的自定义结构?
这让我疯了.
在雨燕2.2,这使得它不可能下标String
有Int
.例如:
let myString = "Test string"
let index = 0
let firstCharacter = myString[index]
Run Code Online (Sandbox Code Playgroud)
这将导致编译错误,说
'subscript'不可用:不能使用Int下标String,请参阅文档注释以供讨论
我看到的一个解决方法是将整数转换为索引类型,但我无法弄清楚如何...
我对Swift 2.2(Xcode 7.3)感到沮丧.要模拟它,只需在用户定义的泛型类中创建一个变量,并从其他地方引用该类.例如:
class A<T> {
let genVar = 1
}
class MyViewController: UIViewController {
let myVar = A<Int>() // crash is here
}
Run Code Online (Sandbox Code Playgroud)
如果您将在运行iOS 7(iPhone 4,在我的情况下)的设备上运行此代码,则会尝试创建泛型类型的变量.以下是设备崩溃日志的第一行:
Exception Type: EXC_BAD_ACCESS (SIGBUS)
Exception Subtype: KERN_PROTECTION_FAILURE at 0x00298910
Triggered by Thread: 0
Thread 0 Crashed:
0 libswiftCore.dylib 0x006b1d64 0x4bd000 + 2051428
1 Phone 0x001c76ec 0xab000 + 1165036
2 libswiftCore.dylib 0x006b307c 0x4bd000 + 2056316
3 libswiftCore.dylib 0x006b2f70 0x4bd000 + 2056048
4 libswiftCore.dylib 0x006b0f24 0x4bd000 + 2047780
5 libswiftCore.dylib 0x006b107c 0x4bd000 + 2048124
6 …
Run Code Online (Sandbox Code Playgroud)