我有最新的xcode测试版,只是尝试在应用内加载网页.
import UIKit
class ViewController: UIViewController {
@IBOutlet var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let url = NSURL (string: "https://www.google.com");
let requestObj = NSURLRequest(URL: url!);
webView.loadRequest(requestObj);
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Run Code Online (Sandbox Code Playgroud)
在这一行:let requestObj = NSURLRequest(URL: url!);
我收到以下错误:
"NSURL"不能隐式转换为"URL"; 你的意思是使用"as"来明确转换?
我删除了感叹号,我收到此错误:
无法转换"NSURL"类型的值?到期望的参数类型"URL"
在过去的四个小时里,我一直在尝试使用互联网上的代码.我在想最新的swift只是不允许这样或者什么,因为没有任何作用.我设置为10.0部署目标和Xcode 8.0兼容.但我也尝试过这两个设置的早期版本,并且我在两个版本的所有版本中都得到了相同的错误.
是的,故事板上的我的webView正确连接到ViewController.swift.
我已经以编程方式创建了一个UIView并添加UIPanGestureRecognizer到它:
class ViewController: UIViewController{
var preludeView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
initViews()
createConstrants()
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: Selector(("handleTap:")))
preludeView.addGestureRecognizer(panGestureRecognizer)
}
func handleTap(recognizer: UIPanGestureRecognizer) {
print("WORKING!!!!")
}
func initViews() {
...
}
func createConstrants() {
...
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我触摸视图时,Xcode会抛出一个错误:
2016-07-13 09:24:29.918 Draft_Hypa_02 [661:83024] - [Draft_Hypa_02.ViewController handleTap:]:无法识别的选择器发送到实例0x17d94a10 2016-07-13 09:24:29.921 Draft_Hypa_02 [661:83024] *终止应用由于未捕获的异常'NSInvalidArgumentException',原因是: ' - [Draft_Hypa_02.ViewController handleTap:]:无法识别的选择发送到实例0x17d94a10'*第一掷调用堆栈:(0x249cf91b 0x2416ae17 0x249d52b5 0x249d2ee1 0x248fe238 0x294ae9eb 0x290e984f 0x28f7aff1 0x294afd4f 0x28f3ba57 0x28f38017 0x28f78ec9 0x28f7867b 0x28f49125 0x28f476d3 0x24991dff 0x249919ed 0x2498fd5b 0x248df229 …
如何String在macOS上获得'Swift3' 的长度?
这曾经在Swift2中工作:
if purchaseDateString.length == 0 {
purchaseDateString = "n/a"
}
Run Code Online (Sandbox Code Playgroud)
什么是Swift3等价于查找String的长度.
UPDATE
马丁指出,这个问题有答案.提到的问题有30个答案.但是,就我所知,它们都没有专门针对Swift3.我错了..
我厌倦了从2016年7月21日到2 016-07-21,但得到了这个日期2015-12-20
这是我试过的代码
let inputFormatter = NSDateFormatter()
inputFormatter.dateFormat = "MM/dd/YYYY"
let outputFormatter = NSDateFormatter()
outputFormatter.dateFormat = "YYYY-MM-dd"
let showDate = inputFormatter.dateFromString("07/21/2016")
let resultString = outputFormatter.stringFromDate(showDate!)
print(resultString)
Run Code Online (Sandbox Code Playgroud)
如何转换?
谢谢
我有协议:
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
Run Code Online (Sandbox Code Playgroud)
我有数据:
let muteForPickerData = ["minute(s)","hour(s)"]
Run Code Online (Sandbox Code Playgroud)
在viewDidLoad我有:
muteForPicker.delegate = self
muteForPicker.dataSource = self
Run Code Online (Sandbox Code Playgroud)
然后我需要方法:
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return muteForPickerData.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return muteForPickerData[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
}
Run Code Online (Sandbox Code Playgroud)
我还是得到了
type viewcontroller不符合协议UIPickerViewDataSource
当我在类的扩展中声明静态参数时,我必须在变量之前编写@nonobjc
@nonobjc static let test = "test"
Run Code Online (Sandbox Code Playgroud)
有时我必须在方法之前编写@objc,所以在swift中使用@objc和@nonobjc.
任何人都可以帮我解决这个问题吗?
我想UITableView在UIView内创建,但它不起作用.
这是我的代码 -
import UIKit
import SnapKit
class ReorderView: UIView, UITableViewDataSource, UITableViewDelegate {
var tableView = UITableView()
let screenHeight = UIScreen.mainScreen().bounds.height
let screenWidth = UIScreen.mainScreen().bounds.width
override init(frame: CGRect){
super.init(frame: frame)
tableView.delegate = self
tableView.dataSource = self
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
setup()
}
func setup() {
self.backgroundColor = UIColor.blackColor()
tableView = UITableView(frame: CGRect(x: 0, y: 0, width: screenWidth*0.5, height: screenHeight))
tableView.layer.backgroundColor = UIColor.blackColor().CGColor
self.addSubview(tableView)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(tableView: UITableView, …Run Code Online (Sandbox Code Playgroud) 我在地图中做了一些注释,当我点击它们时,我看到一些信息,我有一个按钮打开地图,并且有正确的信息,我不能接受它应该画我的路线.
这是我的代码:
我的lat和lon有2个double数组我从查询中填充了它们.
var lat = [Double]()
var lon = [Double]()
Run Code Online (Sandbox Code Playgroud)
这些行用于填充注释
self.annot = Islands(title: object["name"] as! String, subtitle: object["address"] as! String, coordinate: CLLocationCoordinate2D(latitude: (position?.latitude)!, longitude: (position?.longitude)!), info: object["address"] as! String)
self.map.addAnnotations([self.annot])
Run Code Online (Sandbox Code Playgroud)
这是注释创建的位置:
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = "pin"
if annotation.isKindOfClass(MKUserLocation) {
return nil
}
let image = UIImage(named: "car")
let button = UIButton(type: .Custom)
button.frame = CGRectMake(0, 0, 30, 30)
button.setImage(image, forState: .Normal)
button.addTarget(self, action: #selector(Map.info(_:)), forControlEvents:UIControlEvents.TouchUpInside)
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
if …Run Code Online (Sandbox Code Playgroud) 我使用此代码获取当前日期和时间
let today: NSDate = NSDate()
let dateFormatter: NSDateFormatter = NSDateFormatter()
dateFormatter.timeStyle = NSDateFormatterStyle.MediumStyle
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
dateFormatter.timeZone = NSTimeZone(abbreviation: "SGT");
print(dateFormatter.stringFromDate(today))
Run Code Online (Sandbox Code Playgroud)
但我想得到今天约会的开始时间和结束时间
例如:
12-09-2016 00:00:00 AND 12-09-2016 23:59:59
如何获取当前日期的开始和结束时间?
你如何使用Xcode 8和Swift 3在macOS 10.12中使用Copy字符串Clipboard?我找不到任何参考资料.
swift ×8
ios ×6
swift3 ×6
xcode8 ×2
annotations ×1
datetime ×1
dictionary ×1
iphone ×1
macos ×1
macos-sierra ×1
nsdate ×1
nspasteboard ×1
nsurl ×1
objective-c ×1
swift2 ×1
uipickerview ×1
uitableview ×1