此处将为大家介绍关于ios–在Swift2中加载STL对象(金属)的详细内容,并且为您解答有关swiftstructclass的相关问题,此外,我们还将为您介绍关于ios–NSDate()VSNSDa
此处将为大家介绍关于ios – 在Swift 2中加载STL对象(金属)的详细内容,并且为您解答有关swift struct class的相关问题,此外,我们还将为您介绍关于ios – NSDate()VS NSDate.date()在Swift、ios – 在Swift 1.2中将自身作为参数传递给init方法、ios – 在Swift 2.0中以横向模式使用UIImagePickerController、ios – 在swift 2.1中使用MapKit添加不同的引脚颜色的有用信息。
本文目录一览:- ios – 在Swift 2中加载STL对象(金属)(swift struct class)
- ios – NSDate()VS NSDate.date()在Swift
- ios – 在Swift 1.2中将自身作为参数传递给init方法
- ios – 在Swift 2.0中以横向模式使用UIImagePickerController
- ios – 在swift 2.1中使用MapKit添加不同的引脚颜色
ios – 在Swift 2中加载STL对象(金属)(swift struct class)
任何帮助都会很棒.我正在使用swift 2和xCode 7.1.1
解决方法
Apple有一个名为MetalKitEssentials的示例代码项目,其中显示了一些正在运行的内容 – 它是ObjC,但所有相关的API调用在Swift中都是相同的,所以它很容易翻译,特别是如果你已经有一些工作的Swift Metal代码参考.
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.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 – 在Swift 2中加载STL对象(金属)和swift struct class的讲解已经结束,谢谢您的阅读,如果想了解更多关于ios – NSDate()VS NSDate.date()在Swift、ios – 在Swift 1.2中将自身作为参数传递给init方法、ios – 在Swift 2.0中以横向模式使用UIImagePickerController、ios – 在swift 2.1中使用MapKit添加不同的引脚颜色的相关知识,请在本站搜索。
本文标签: