Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 类别';ViewController';没有初始值设定项错误:xcode Beta 4_Ios_Xcode_Swift - Fatal编程技术网

Ios 类别';ViewController';没有初始值设定项错误:xcode Beta 4

Ios 类别';ViewController';没有初始值设定项错误:xcode Beta 4,ios,xcode,swift,Ios,Xcode,Swift,我在一个文件上收到了这个错误,这个文件以前在所有其他beta版本上都可以使用,出于某种原因,在beta 4 xcode中它显示了这个错误,这是整个项目中唯一一个这样做的文件,我不确定为什么它只在beta 4版本上使用,有什么想法吗?我已经尝试在它上面使用默认的init,但是我得到了一个编译错误 我认为有太多的代码要放进去,所以这里是直接的文件 并显示tableview: func tableView(tableView: UITableView!, heightForHeaderInSecti

我在一个文件上收到了这个错误,这个文件以前在所有其他beta版本上都可以使用,出于某种原因,在beta 4 xcode中它显示了这个错误,这是整个项目中唯一一个这样做的文件,我不确定为什么它只在beta 4版本上使用,有什么想法吗?我已经尝试在它上面使用默认的init,但是我得到了一个编译错误

我认为有太多的代码要放进去,所以这里是直接的文件

并显示tableview:

func tableView(tableView: UITableView!, heightForHeaderInSection section: Int) -> Int {
    return 1
}
func tableView(tableView: UITableView!, viewForFooterInSection section: Int) -> UIView {
    var view = UIView(frame: CGRect.zeroRect)
    return view
}
func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
    return 81
}

func tableView(tableView: UITableView!, heightForFootInSection section: Int) -> Int {
        return 1
}

func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {

    if self.tableData.count > firstAmount {
        return firstAmount + 1
    } else {
        return 0
    }
        //return self.tableData!.count


}

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!  {
    let CellIndentifier: NSString = "Cell"
    let moreID: String = "moreCell"

    var searchCell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as SearchViewCell


    var rowData: NSDictionary = self.tableData[indexPath!.row] as NSDictionary
    var rows = indexPath.row

    if rows == firstAmount {

        searchCell.loadMoreView.hidden = false

    } else {
    searchCell.selectionStyle = UITableViewCellSelectionStyle.None
    searchCell.clipsToBounds = true

        searchCell.loadMoreView.hidden = true

    var artistName = rowData["artistName"] as String
    var trackName = rowData["trackName"] as String
    var previewURL = rowData["previewUrl"] as String
    //var artistURL = rowData["artistViewUrl"] as String
    //var buyURL = rowData["trackViewUrl"] as String
    var artworkURL = rowData["artworkUrl100"] as String


    searchCell.previewButton.addTarget(self, action: "flipButton:", forControlEvents: UIControlEvents.TouchUpInside)


    searchCell.songName.text = trackName
    searchCell.artistName.text = artistName

    var layer: CALayer = searchCell.blurView.layer
    layer.shadowOffset = CGSizeMake(0, 0)
    layer.shadowColor = UIColor(white: 0.0, alpha: 1.0).CGColor
    layer.shadowRadius = 1.0
    layer.shadowOpacity = 0.5

    searchCell.backImage.clipsToBounds = true
    var visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .Dark)) as UIVisualEffectView


    visualEffectView.frame = searchCell.blurView.bounds

    searchCell.blurView.addSubview(visualEffectView)
    searchCell.blurView.sendSubviewToBack(visualEffectView)


       // NSLayoutConstraint.deactivateConstraints(searchCell.blurView.constraints())
        searchCell.blurView.addConstraint(NSLayoutConstraint(
            item:visualEffectView, attribute:.CenterX,
            relatedBy:.Equal, toItem:searchCell.blurView,
            attribute:.CenterX, multiplier:1, constant:0))
        searchCell.blurView.addConstraint(NSLayoutConstraint(
            item:visualEffectView, attribute:.CenterY,
            relatedBy:.Equal, toItem:searchCell.blurView,
            attribute:.CenterY, multiplier:1, constant:0))

        searchCell.blurView.addConstraint(NSLayoutConstraint(
            item:visualEffectView, attribute:.Width,
            relatedBy:.Equal, toItem:searchCell.blurView,
            attribute:.Width, multiplier:1, constant:0))

        searchCell.blurView.addConstraint(NSLayoutConstraint(
            item:visualEffectView, attribute:.Height,
            relatedBy:.Equal, toItem:searchCell.blurView,
            attribute:.Height, multiplier:1, constant:0))

        searchCell.blurView.addConstraint(NSLayoutConstraint(
            item:visualEffectView, attribute:.Leading,
            relatedBy:.Equal, toItem:searchCell.blurView,
            attribute:.Leading, multiplier:1, constant:0))

        searchCell.blurView.addConstraint(NSLayoutConstraint(
            item:visualEffectView, attribute:.Trailing,
            relatedBy:.Equal, toItem:searchCell.blurView,
            attribute:.Trailing, multiplier:1, constant:0))


        visualEffectView.setTranslatesAutoresizingMaskIntoConstraints(false)




    var placeHolder = UIImage(named: "placeHolder.png")
    searchCell.artworkIMG.image = placeHolder
    searchCell.backImage.image = placeHolder

        dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {

            var urlString: NSString = rowData["artworkUrl100"] as NSString

            var image: UIImage? = self.imageCache.valueForKey(urlString) as? UIImage

            if( !image? ) {
                var imgURL: NSURL = NSURL(string: urlString)

                var request: NSURLRequest = NSURLRequest(URL: imgURL)
                var urlConnection: NSURLConnection = NSURLConnection(request: request, delegate: self)

                NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in
                    if !error? {
                        image = UIImage(data: data)
                        self.imageCache.setValue(image, forKey: urlString)
                        searchCell.artworkIMG.image = image
                        searchCell.backImage.image = image
                    } else {
                        println("Error: \(error.localizedDescription)")
                    }
                    })

            } else {

                searchCell.artworkIMG.image = image
                searchCell.backImage.image = image
            }
            })

    }
    return searchCell
}
func tableView(tableView: UITableView!, willDisplayCell cell: UITableViewCell!, forRowAtIndexPath indexPath: NSIndexPath!) {
    cell.layer.transform = CATransform3DMakeScale(0.1,0.1,1)
    /*UIView.animateWithDuration(0.25, animations: {
        cell.layer.transform = CATransform3DMakeScale(1,1,1)
        })*/
    UIView.animateWithDuration(0.4, animations: {
        cell.layer.transform = CATransform3DMakeScale(1.1,1.1,1.1)
        }, completion: {(value: Bool) in
        cell.layer.transform = CATransform3DMakeScale(1,1,1)
        })
}
从SwiftSoda.com:

许多Swift程序员在使用ViewController和Xcode beta 4时遇到了一些小问题。错误是viewController不能用()构造

但如果您查看视图控制器,您将看到另一个错误,类viewController没有初始值设定项。后跟(替换为您正在使用的任何插座)NSTextField具有非可选类型“NSTextField”

将IBO插座设置为?结尾纠正了问题。之后,每次将Outlet的变量与!一起使用时,都需要更改一些语法

下面是一个简单的例子:

//ViewController Class File:
// view controller swift file for OS X
import AppKit

// you can use () for AnyObject as long as you do some things first.
var mvc = vc()

class vc: NSViewController {
// the key here is "?" after NSTextField to declare an optional
@IBOutlet var myStatusField: NSTextField?
}

//AppDelegate File:
import AppKit

//here we load the view inside the AppDelegate
class AppDelegate: NSObject, NSApplicationDelegate {

@IBOutlet var window: NSWindow!

func applicationDidFinishLaunching(aNotification: NSNotification?) {
// Insert code here to initialize your application

//load and center view in main window
window.contentView.addSubview(mvc.view)
mvc.view.frame = window.contentView.bounds

//here we display some text in myStatusField with *!.stringValue
// ! is used to unwrap the optional.
mvc.myStatusField!.stringValue = "Swift Soda wishes you a good day!"
}
}

您可能希望使用if IBOutletName语句安全地展开任何选项,如下所示:

if mvc.myStatusField {
mvc.myStatusField!.stringValue = "Swift Soda wishes you a good day!"
}
这样你的应用程序就不会崩溃


我想让IBOutlets不使用?还有!(可选),但到目前为止,它似乎是唯一在beta4中与NSViewController一起工作的方法。

我认为最简单的方法就是不使用override关键字重写init函数,只调用super.init,它就工作了 像这样

@IBOutlet weak var tableView: UITableView!

init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)

}

由于我的一个属性未初始化,因此出现了相同的错误。希望它能帮助一些人在视图控制器中实现以下功能:

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

我之所以出现此错误,是因为我声明常量为可选,您可以看到下面的代码:

let subCategory: String?
我将表单
let
更改为
var
,并为我解决了问题。
使用Xcode 10,Swift 4.2

代码太多了?链接到文件会更好吗?您可以显示相关部分而不显示所有部分。例如,显示您的类及其至少第一行初始值设定项…代码已添加@jtbandesThat worked!现在我遇到了另一个问题,我将打开另一个线程。谢谢用感叹号代替问号对我很有效。对我很有效。。。谢谢我的viewcontroller中的一个属性未初始化。我将此属性设置为默认值,错误消失。似乎应该初始化未初始化的变量,或者您应该指定它们是非强制性的?这对我很有用。我有一个未初始化的属性,所以我将变量设置为可选(?),它可以工作。
let subCategory: String?