小编Jos*_*nor的帖子

调用Swift中的前一个视图控制器

我试图调用根控制器,我不知道调用它的代码.我正在使用条件,它将检查以前的视图控制器是什么,并且将根据它是什么来运行代码块.这是我的代码:

SecondViewController

override func viewDidLoad ( ) {
     super.viewDidLoad ( )

     let previousController = storyboard!.instantiateViewControllerWithIdentifier ("firstViewController") as? FirstViewController

     //HELP IN LINE BELOW
     //if root view controller == previousController {
              //run some code
     //}
}
Run Code Online (Sandbox Code Playgroud)

iphone ios swift

1
推荐指数
1
解决办法
1009
查看次数

NSDate代码无法迁移到Swift 3.0

我试图将我的代码转换为Swift 3.0,并且在使用迁移工具后我无法转换某个代码块.

以前的SWIFT 3.0迁移:

import Foundation

extension NSDate {
    convenience init(posixTime: Double) {
        self.init(timeIntervalSince1970: Double(posixTime) / 1000.0)
    }
}
Run Code Online (Sandbox Code Playgroud)

迁移工具之后:

extension Date {
    init(posixTime: Double) {
        //ERROR IN THE LINE BELOW: "'init' is a member of the type; use 'type(of: ...)' to initialize a new object of the same dynamic type"
        (self as NSDate).init(timeIntervalSince1970: Double(posixTime) / 1000.0)
    }
}
Run Code Online (Sandbox Code Playgroud)

我按照Xcodes的建议将"init"替换为"type(of:init)":

extension Date {
    init(posixTime: Double) {
        //ERROR IN THE LINE BELOW: "Expected expression in list of expressions"
        (self as NSDate).type(of: …
Run Code Online (Sandbox Code Playgroud)

ios swift3

1
推荐指数
1
解决办法
397
查看次数

仅更新一次位置

我正在为我的应用程序添加一个 mapKit,并将地图的中间设置为人员当前位置(以纬度和经度为单位)。我能够让它工作,但是,它经常更新。我只希望它在加载应用程序时更新一次,然后不再更新。可能每2分钟更新一次。这是我的代码:

func locationManager(manager: CLLocationManager!,
    didUpdateLocations locations: [AnyObject]!)
{
    var latestLocation = locations.last as! CLLocation


    let location = CLLocationCoordinate2D(latitude: latestLocation.coordinate.latitude, longitude: latestLocation.coordinate.longitude)
    let span = MKCoordinateSpanMake(0.015, 0.015)

    //Let our point be the center of our span
    //region holds value of span and location
    let region = MKCoordinateRegion(center: location, span: span)

    //set up region on mapView
    mapView.setRegion(region, animated: true)

    //MKPointAnnotation defines a concrete annotation
    let annotation = MKPointAnnotation()
}
Run Code Online (Sandbox Code Playgroud)

iphone mapkit ios swift

0
推荐指数
1
解决办法
1077
查看次数

不清楚了解?:用迅捷的语法

我在return语句中无法理解这种语法.我不确定它是否是Swift 2.0中的新功能,但它是什么?和:意思是?这个问号是否可选,即使间隔?我很困惑,来自Objective-C背景.

private func doContainsUser(user: User) -> Bool {
    let isInverted = setOfDiff.contains(user)
    let wasInitiallyAdded = setOfCircleUsers.contains(user)

    //What does the ? and the : mean?
    return isInverted ? !wasInitiallyAdded : wasInitiallyAdded
}
Run Code Online (Sandbox Code Playgroud)

syntax swift

-7
推荐指数
1
解决办法
98
查看次数

标签 统计

ios ×3

swift ×3

iphone ×2

mapkit ×1

swift3 ×1

syntax ×1