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
- class viewcontroller has no initializers 如何fixed
- class viewController has no initializers解决办法
- class viewController has no initializers解决办法
- class viewController has no initializers解决办法
- swift class viewController has no initializers 的解决方法
- class has no initializers
- Class has no initializers: Swift Error
- 'class QApplication' has no member named 'setMainWidget'
- class has no zero argument constructor
- error: ‘class QApplication’ has no member named ‘setMainWidget’
- 错误:'class QApplication' has no member named 'setMainwidget'
- Subresource for target class has no jax-rs annotations
- No message body writer has been found for class
- ViewController的Custom class
- JSON: Property 'xxx' has no getter method in class 'class xxx'
- Qt: error: 'class QString' has no member named 'toAscii'(class QChar)
- without base relocation /fixed:no
- no class
- HTTP 协议之压缩
- Spark的RDD原理以及2.0特性的介绍
- 在Activity和Service之间使用Binder和回调接口进行通信
- CF #357 div2 B Economy Game
- [leetcode] 【字符串】 49. Group Anagrams
- class viewcontroller has no initializers 如何fixed
- Android初级教程短信防火墙
- maven 项目导入之后转成web项目,添加到tomcat中发布
- Espresso简介
- java学习--面向对象
- JSON数据中带有HTML标签解决方法
- 自己封装的一个工具类
- HTTP 协议详解
- 一丶一步一步的来学习.net框架,什么是CIL?