我正在按照此答案来创建自定义UITextField,但是无法获得相同的结果,这是我的操作方法:
方法1(与原始答案完全一样):
extension UITextField {
func useUnderLine() {
let border = CALayer()
let borderWidth = CGFloat(1.0)
border.borderColor = UIColor.white.cgColor
border.frame = CGRect(x: 0, y: self.frame.size.height - borderWidth, width: self.frame.size.width, height: self.frame.size.height)
border.borderWidth = borderWidth
self.layer.addSublayer(border)
self.layer.masksToBounds = true
}
}
override func viewDidLoad() {
super.viewDidLoad()
firstTextField.useUnderLine()
}
Run Code Online (Sandbox Code Playgroud)
方法2(使用自定义类):
class CustomTextField: UITextField {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
let borderWidth = CGFloat(1.0)
self.layer.borderColor = UIColor.white.cgColor
self.layer.frame = CGRect(x: 0, y: self.frame.size.height - borderWidth, width: self.frame.size.width, …Run Code Online (Sandbox Code Playgroud) 我正在尝试将视图控制器显示为弹出窗口,当它全屏显示时,它在 iPhone 上工作正常,但在 iPad 上崩溃。
@IBAction func selectOpponentItems(_ sender: UIButton) {
let VC = storyboard?.instantiateViewController(withIdentifier: "ItemSelectionVC") as! ItemSelectionVC
// Error here
VC.delegate = self
VC.preferredContentSize = CGSize(width: UIScreen.main.bounds.width / 2, height: UIScreen.main.bounds.height / 2)
VC.modalPresentationStyle = UIModalPresentationStyle.popover
self.present(VC, animated: true, completion: nil)
}
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
Run Code Online (Sandbox Code Playgroud)
错误消息显示:
无法将类型“UIViewController”(0x1b1e33f60)的值转换为“Overpower.ItemSelectionVC”(0x10005ad40)。
我从Github分叉了一个项目,Xcode显示了很多警告:
'M_PI'已弃用:请使用'Double.pi'或'.pi'获取正确类型的值并避免投射.
和
'M_PI_2'已弃用:请使用'Double.pi'或'.pi'获取正确类型的值并避免投射.
由于两个M_PI和M_PI_2提示被替换Double.pi,我认为有实际上相同的值.但是,项目中有这个代码:
switch angle {
case M_PI_2:
...
case M_PI:
...
case Double.pi * 3:
...
default:
...
}
Run Code Online (Sandbox Code Playgroud)
我真的很困惑,是M_PI和M_PI_2不同的?还是他们一样?
更新:
事实证明这是我的错误,Xcode说'M_PI_2'已被弃用:请使用Double.pi / 2或.pi / 2获取正确类型的值并避免投射.所以它不是一个bug,只是太难注意到2个提示的区别.
我创建了一个新项目,ViewController.swift 是:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Run Code Online (Sandbox Code Playgroud)
我在视图控制器中创建了一个表视图,所以我想将类类型更改为 UITableViewController,这是我所做的:
class ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated. …Run Code Online (Sandbox Code Playgroud) 有时我在将变量定义为 时遇到错误NSArray/String/URL,通常可以通过将其更改为 来解决NSMutableArray/String/URL。
它们之间有什么区别?人们说你不能改变 a 的值NSString,但既然我将它定义为带有 a 的变量,为什么不呢var?
请使用 Swift 2 代码来解释。
这是代码示例:
enum Router: URLRequestConvertible {
static let baseURLString = "https://api.500px.com/v1"
static let consumerKey = "My_Key"
case PopularPhotos(Int)
case PhotoInfo(Int, ImageSize)
case Comments(Int, Int)
var URLRequest: NSURLRequest {
//Error: Type does not conform to protocol 'URLRequestConvertible' with Alamofire.
//I solved this problem by changing it to NSMutableRequest.
let (path, parameters) : (String, [String: AnyObject]) = {
switch self {
//3 cases. …Run Code Online (Sandbox Code Playgroud) 如果我有一个类Christmas和一个协议Merry,为了使Christmas符合Merry,许多人会这样做:
class Christmas {
...
}
extension Christmas: Merry {
...
}
Run Code Online (Sandbox Code Playgroud)
它也受到Apple的鼓励.
但是,在定义类时,让类符合协议是不是更方便?像这样:
class Christmas: Merry {
...
}
Run Code Online (Sandbox Code Playgroud)
两种方法有什么区别?