本篇文章给大家谈谈ios–在swift2.1中使用MapKit添加不同的引脚颜色,同时本文还将给你拓展ios–NSDate()VSNSDate.date()在Swift、ios–在Swift1.2中将
本篇文章给大家谈谈ios – 在swift 2.1中使用MapKit添加不同的引脚颜色,同时本文还将给你拓展ios – NSDate()VS NSDate.date()在Swift、ios – 在Swift 1.2中将自身作为参数传递给init方法、ios – 在Swift 2.0中以横向模式使用UIImagePickerController、ios – 在swift 2.2中不推荐使用ContentsOfFile方法等相关知识,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:- ios – 在swift 2.1中使用MapKit添加不同的引脚颜色
- ios – NSDate()VS NSDate.date()在Swift
- ios – 在Swift 1.2中将自身作为参数传递给init方法
- ios – 在Swift 2.0中以横向模式使用UIImagePickerController
- ios – 在swift 2.2中不推荐使用ContentsOfFile方法
ios – 在swift 2.1中使用MapKit添加不同的引脚颜色
import UIKit import MapKit class MapsViewController: UIViewController,MKMapViewDelegate{ var shops: NSArray? { didSet{ self.loadMaps() } } @IBOutlet weak var map: MKMapView? override func viewDidLoad() { super.viewDidLoad() loadMaps() self.title = "Carte" self.map!.delegate = self } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // dispose of any resources that can be recreated. } func mapView(mapView: MKMapView,viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { // simple and inefficient example let annotationView = MKPinAnnotationView() annotationView.pinTintColor = UIColor.purpleColor() return annotationView } func loadMaps(){ // navigationController?.navigationBar.topItem!.title = "Carte" let shopsArray = self.shops! as NSArray for shop in shopsArray { let location = CLLocationCoordinate2D( latitude: shop["lat"] as! Double,longitude: shop["long"] as! Double ) let annotation = MKPointAnnotation() annotation.coordinate = location annotation.title = shop["name"] as? String annotation.subtitle = shop["addresse"] as? String map?.addAnnotation(annotation) } // add point } /* // MARK: - Navigation // In a storyboard-based application,you will often want to do a little preparation before navigation override func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ }
解决方法
在自定义类中,添加一个属性,比如pinColor,您可以使用该属性来自定义注释的颜色.
此示例是MKPointAnnotation的子类:
import UIKit import MapKit class ColorPointAnnotation: MKPointAnnotation { var pinColor: UIColor init(pinColor: UIColor) { self.pinColor = pinColor super.init() } }
创建ColorPointAnnotation类型的注释并设置其pinColor:
let annotation = ColorPointAnnotation(pinColor: UIColor.blueColor()) annotation.coordinate = coordinate annotation.title = "title" annotation.subtitle = "subtitle" self.mapView.addAnnotation(annotation)
在viewForAnnotation中,使用pinColor属性设置视图的pinTintColor:
func mapView(mapView: MKMapView,viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { if annotation is MKUserLocation { return nil } let reuseId = "pin" var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView if pinView == nil { pinView = MKPinAnnotationView(annotation: annotation,reuseIdentifier: reuseId) let colorPointAnnotation = annotation as! ColorPointAnnotation pinView?.pinTintColor = colorPointAnnotation.pinColor } else { pinView?.annotation = annotation } return pinView }
ios – NSDate()VS NSDate.date()在Swift
lastTick = NSDate.date()
这会导致编译错误:
'date()' is unavailable: use object construction 'NSDate()'
应该等于:
NSDate *lastTick = [NSDate date];
(从NSDate reference)
Apple改变了Swift interface to NSDate,因为我看到使用NSDate.date的其他例子?
这只是NSDate或者你不能为任何Objective-C API调用类型方法?
解决方法
如果你阅读了“使用Swift与Cocoa和Objective-C”指南,有一个与Objective-C apis交互的部分:
For consistency and simplicity,Objective-C factory methods get mapped as convenience initializers in Swift. This mapping allows them to be used with the same concise,clear Syntax as initializers.”
Excerpt From: Apple Inc. “Using Swift with Cocoa and Objective-C.” iBooks. 07000
所以工厂方法:
[NSDate date]
在Swift中转换为初始化程序
NSDate()
它不只是NSDate你会发现这种模式,但在其他Cocoa API的工厂方法。
ios – 在Swift 1.2中将自身作为参数传递给init方法
class SubView: UIView { let pandGestureRecognizer: UIPanGestureRecognizer! required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) self.pandGestureRecognizer = UIPanGestureRecognizer(target: self,action: "panAction:") } func panAction(gesture: UIPanGestureRecognizer) { // ... } }
更新到Xcode 6.3(使用Swift 1.2)后,会发生以下编译错误:
Property 'self.panGestureRecognizer' not initialized at super.init call Immutable value 'self.panGestureRecognizer' may only be initialized once
在super.init调用之前移动以下行:
self.pandGestureRecognizer = UIPanGestureRecognizer(target: self,action: "panAction:")
给出以下错误:
'self' is used before super.init call
属性’panGestureRecognizer’不需要变异,因此必须将其声明为常量’let’.由于它是常量,因此必须在声明时具有初始值,或者在init方法中初始化它.要初始化它,需要在’target’参数中传递’self’.
其他线程建议将其声明为隐式解包可选,并在super.init调用后初始化它.这之前一直有效,直到我更新到Xcode 6.3.
有没有人知道这种情况的正确实施或解决方法?
解决方法
问题是你使用let-optionals声明为let没有给出默认值nil(然而var是).以下是在Swift 1.2中引入的,否则将无效,因为在声明之后你将无法给myOptional一个值:
let myOptional: Int? if myCondition { myOptional = 1 } else { myOptional = nil }
因此,你得到错误’属性’self.panGestureRecognizer’未在super.init调用’初始化’因为在调用super.init(coder:aDecoder)之前,因为panGestureRecognizer不是nil;它根本没有初始化.
解决方案:
1.将panGestureRecognizer声明为var,意味着它将被赋予默认值nil,然后在调用super.init(coder:aDecoder)之后可以更改.
2.在我看来,更好的解决方案是:不要使用隐式展开的可选项,并使用初始值UIPanGestureRecognizer()声明panGestureRecognizer.然后在调用super.init后设置目标:
class SubView: UIView { let panGestureRecognizer = UIPanGestureRecognizer() required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) panGestureRecognizer.addTarget(self,action: Selector("panAction:")) } }
ios – 在Swift 2.0中以横向模式使用UIImagePickerController
这是我的代码:
class signUpViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate { func imagePickerController(picker: UIImagePickerController,didFinishPickingImage image: UIImage!,editingInfo: [NSObject : AnyObject]!) { print("Image Selected") self.dismissViewControllerAnimated(true,completion: nil) profileImageView.image = image } @IBAction func importimage(sender: AnyObject) { var image = UIImagePickerController() image.delegate = self image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary image.allowsEditing = false self.presentViewController(image,animated: true,completion: nil) } }
解决方法
extension UIImagePickerController { public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { return .Landscape } }
这使得所有UIImagePickerControllers都在您的应用程序环境中.您也可以对其进行子类化并覆盖此方法,以仅使子类具有横向能力:
class LandscapePickerController: UIImagePickerController { public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { return .Landscape } }
最后,为了支持所有方向,您可以返回
return [.Landscape,.Portrait]
对于Swift 3:
extension UIImagePickerController { override open var shouldAutorotate: Bool { return true } override open var supportedInterfaceOrientations : UIInterfaceOrientationMask { return .all } }
ios – 在swift 2.2中不推荐使用ContentsOfFile方法
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); Nsstring *documentsDirectory = [paths objectAtIndex:0]; Nsstring *filePath = [documentsDirectory stringByAppendingPathComponent:@"Logger.txt"]; Nsstring *content = [Nsstring stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
当我尝试将其转换为swift时,我无法转换它,因为不推荐使用ContentsOfFile方法.任何人都可以告诉我如何将其转换为swift?我正在使用Xcode 7.2和swift 2.2.我在swift中的代码是
var paths : NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true) var documentsDirectory : Nsstring = paths[0] as! String var filePath = documentsDirectory.stringByAppendingPathComponent("Logger.txt") var error:NSError? let content = Nsstring(contentsOfFile: filePath,encoding:NSUTF8StringEncoding,error: &error) if let theError = error { NSLog("\(theError.localizedDescription)") }
解决方法
let path:String = NSBundle.mainBundle().pathForResource("Logger",ofType: "txt")! txtContent.text = try? String(contentsOfFile: path,encoding: NSUTF8StringEncoding)
斯威夫特3
let path:String = Bundle.main.path(forResource: "Logger",encoding: String.Encoding.utf8)
今天关于ios – 在swift 2.1中使用MapKit添加不同的引脚颜色的讲解已经结束,谢谢您的阅读,如果想了解更多关于ios – NSDate()VS NSDate.date()在Swift、ios – 在Swift 1.2中将自身作为参数传递给init方法、ios – 在Swift 2.0中以横向模式使用UIImagePickerController、ios – 在swift 2.2中不推荐使用ContentsOfFile方法的相关知识,请在本站搜索。
本文标签: