Swift2 - 条件绑定的初始化程序必须具有Optional类型,而不是'String'

时间:2015-10-29 06:44:51

标签: swift2

我收到此错误。我发现了类似的代码。我尝试修复但我无法解决。这是我的代码

    let path = docDir.stringByAppendingPathComponent(dbName)
    let fm = NSFileManager.defaultManager()
    if !(fm.fileExistsAtPath(path)) {


if let from = (NSBundle.mainBundle().resourcePath! as NSString).stringByAppendingPathComponent(dbName)

{
             var error:NSError?
             do {
                try fm.copyItemAtPath(from, toPath: path)
            } catch let error1 as NSError {
                error = error1
                print("SQLiteDB - failed to copy writable version of DB!")
                print("Error - \(error!.localizedDescription)")
                return 
            }
        }
    }

错误在线

if let from = (NSBundle.mainBundle().resourcePath! as NSString).stringByAppendingPathComponent(dbName)

报告错误:

  

条件绑定的初始化程序必须具有Optional类型,而不是'String'

请有人帮帮我。非常感谢

3 个答案:

答案 0 :(得分:2)

使用如下。

let from:NSString = NSBundle.mainBundle().resourcePath!.stringByAppendingPathComponent(dbPath)
        do {
            try fileManager.copyItemAtPath(from as String, toPath: toPath as String)

        } catch let error1 as NSError {
            error = error1
        }

答案 1 :(得分:1)

请指定变量类型

 if let imageURL:String = array[indexPath.row].absoluteString {
    //Do something
    }

答案 2 :(得分:0)

这是因为stringByAppendingPathComponent只返回String,而不是可选字符串String?。因此,您无法在Stringif let使用let打开func stringByAppendingPathComponent(_ str: String) -> String

let from = (NSBundle.mainBundle().resourcePath! as NSString).stringByAppendingPathComponent(dbName)

var error:NSError?
    do {
        ...
    <!--INDEX file-->
    <!DOCTYPE html>
    <html>

    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
        <title></title>
        <link href="lib/ionic/css/ionic.css" rel="stylesheet">
        <link href="css/style.css" rel="stylesheet">

       <!-- ionic/angularjs js -->
        <script src="js/angular.min.js"></script>
        <script src="js/jquery.min.js"></script>
        <script src="js/jquery-ui.js"></script>
        <script src="lib/ionic/js/ionic.bundle.js"></script>
        <script src="js/ng-cordova.min.js"></script>

        <script src="Module.js"></script>
        <script src="cordova.js"></script>
        <script src="js/app.js"></script>
    </head>

    <body ng-app="starter" ng-controller="DeviceController">
        <ion-pane>
            <ion-header-bar class="bar-stable">
                <h1 class="title">Device Information</h1>
            </ion-header-bar>
            <ion-content>
                <div class="item item-text-wrap">
                    <ul class="list">
                        <li class="item">
                            Manufacturer : {{manufacturer}}
                        </li>
                        <li class="item">
                            Model : {{model}}
                        </li>
                        <li class="item">
                            Platform : {{platform}}
                        </li>
                        <li class="item">
                            UUID : {{uuid}}
                        </li>
                    </ul>
                </div>
            </ion-content>
        </ion-pane>
    </body>
    </html>

    <!--Module.js -->
    var deviceTracker = angular.module('starter', ['ionic','ngCordova'])

    .run(function($ionicPlatform) {
      $ionicPlatform.ready(function() {
        // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
        // for form inputs)
        if(window.cordova && window.cordova.plugins.Keyboard) {
          cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        }
        if(window.StatusBar) {
          StatusBar.styleDefault();
        }
      });
    })

    <!-- Device tracker Controller -->
    deviceTracker.controller('DeviceController', function($ionicPlatform, $scope, $cordovaDevice) {
    $ionicPlatform.ready(function() {
    $scope.$apply(function() {
    // sometimes binding does not work! :/
    // getting device infor from $cordovaDevice
    var device = $cordovaDevice.getDevice();
    $scope.manufacturer = device.manufacturer;
    $scope.model = device.model;
    $scope.platform = device.platform;
    $scope.uuid = device.uuid;
    });
    });
    })

I am Getting an error Like this....

GET http://127.0.0.1:49943/cordova.js 

angular.min.js:102 ReferenceError: device is not defined
    at Object.getDevice (ng-cordova.min.js:7)
    at app.js:26
    at l.$eval (angular.min.js:125)
    at l.$apply (angular.min.js:126)
    at app.js:23
    at Array.<anonymous> (ionic.bundle.js:48811)
    at onPlatformReady (ionic.bundle.js:2447)
    at onWindowLoad (ionic.bundle.js:2428)