class viewcontroller has no initializers 如何fixed

来源:互联网 发布:如何在淘宝开网店 编辑:程序博客网 时间:2024/06/05 17:06


import UIKitclass ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {        var files: NSMutableArray = []    var fileManager:NSFileManager    var documentsPath: String!    var filelist:NSArray!
        @IBOutlet weak var tabla: UITableView!        override func viewDidLoad() {        super.viewDidLoad()                files = []        fileManager = NSFileManager.defaultManager()        documentsPath = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).first! as String)        filelist = try! fileManager.contentsOfDirectoryAtPath(documentsPath)                print("documentspath:  \(documentsPath)")        print(files.count)    }        override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()    }        override func viewDidAppear(animated: Bool) {        filelist = try! fileManager.contentsOfDirectoryAtPath(documentsPath)        files = []        for file in filelist {            files.addObject(file)            print("file:  \(file)")        }        tabla.reloadData()    }    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {        return files.count    }        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {        let identifier:NSString = "CollectionCell"        let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier(identifier as String, forIndexPath: indexPath)         cell.textLabel!.text = files.objectAtIndex(indexPath.row).description                var isDir: ObjCBool = ObjCBool(false)        if(fileManager.fileExistsAtPath((documentsPath as NSString).stringByAppendingPathComponent(files[indexPath.row] as! String), isDirectory: &isDir)){            if(isDir){                cell.imageView!.image = UIImage(named: "dir.png")            } else{                cell.imageView!.image = UIImage(named: "file.png")            }        }        return cell    }}

编译报错



因为Swift中要求变量或常量在声明时就要初始化其值,所以我们在实际开发中,声明变量或常量时使用可选类型。

?
1
2
3
4
var stitle : UILabel?
var webview : UIWebView?
var waitflag : UIActivityIndicatorView?
var domain :String?


后面代码中使用到以上变量时需要加 ! 对其解包。  否则的话就用可选值?

比如下面,写一个UIPageViewController

property pageViewController每声明一个可选值,以后每次使用pageViewController都要进行!解包
声明为可选,以后

class RootViewController: UIViewController, UIPageViewControllerDelegate {        var pageViewController: UIPageViewController?    lazy var modelController = ModelController()        override func viewDidLoad() {        super.viewDidLoad()                // Configure the page view controller and add it as a child view controller.        self.pageViewController = UIPageViewController(transitionStyle: .PageCurl, navigationOrientation: .Horizontal, options: nil)        self.pageViewController!.delegate = self                let startingViewController: DataViewController = self.modelController.viewControllerAtIndex(0, storyboard: self.storyboard!)!        let viewControllers = [startingViewController]        self.pageViewController!.setViewControllers(viewControllers, direction: .Forward, animated: false, completion: {done in })                self.pageViewController!.dataSource = self.modelController                self.addChildViewController(self.pageViewController!)        self.view.addSubview(self.pageViewController!.view)                // Set the page view controller's bounds using an inset rect so that self's view is visible around the edges of the pages.        var pageViewRect = self.view.bounds        if UIDevice.currentDevice().userInterfaceIdiom == .Pad {            pageViewRect = CGRectInset(pageViewRect, 40.0, 40.0)        }        self.pageViewController!.view.frame = pageViewRect                self.pageViewController!.didMoveToParentViewController(self)                // Add the page view controller's gesture recognizers to the book view controller's view so that the gestures are started more easily.        self.view.gestureRecognizers = self.pageViewController!.gestureRecognizers    }        override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()    }        // MARK: - UIPageViewController delegate methods    func pageViewController(pageViewController: UIPageViewController, spineLocationForInterfaceOrientation orientation: UIInterfaceOrientation) -> UIPageViewControllerSpineLocation {        if (orientation == .Portrait) || (orientation == .PortraitUpsideDown) || (UIDevice.currentDevice().userInterfaceIdiom == .Phone) {            // In portrait orientation or on iPhone: Set the spine position to "min" and the page view controller's view controllers array to contain just one view controller. Setting the spine position to 'UIPageViewControllerSpineLocationMid' in landscape orientation sets the doubleSided property to true, so set it to false here.            let currentViewController = self.pageViewController!.viewControllers![0]            let viewControllers = [currentViewController]            self.pageViewController!.setViewControllers(viewControllers, direction: .Forward, animated: true, completion: {done in })                        self.pageViewController!.doubleSided = false            return .Min        }                // In landscape orientation: Set set the spine location to "mid" and the page view controller's view controllers array to contain two view controllers. If the current page is even, set it to contain the current and next view controllers; if it is odd, set the array to contain the previous and current view controllers.        let currentViewController = self.pageViewController!.viewControllers![0] as! DataViewController        var viewControllers: [UIViewController]                let indexOfCurrentViewController = self.modelController.indexOfViewController(currentViewController)        if (indexOfCurrentViewController == 0) || (indexOfCurrentViewController % 2 == 0) {            let nextViewController = self.modelController.pageViewController(self.pageViewController!, viewControllerAfterViewController: currentViewController)            viewControllers = [currentViewController, nextViewController!]        } else {            let previousViewController = self.modelController.pageViewController(self.pageViewController!, viewControllerBeforeViewController: currentViewController)            viewControllers = [previousViewController!, currentViewController]        }        self.pageViewController!.setViewControllers(viewControllers, direction: .Forward, animated: true, completion: {done in })                return .Mid    }    }
运行结果




0 0
原创粉丝点击