Swift 3项目中的几个错误

时间:2017-01-25 03:23:58

标签: ios swift

我得到的四个错误是

1:Use of undeclared type 'MemeImage' - Line 46

2:Binary Operator '*' cannot be applied to operands of type '()' and 'Int' - Line 85

3:Unexpected non-void return value in void function - Line 113

4:Use of local variable 'makeMemedImage' before its declaration - Line 169

代码:

import UIKit

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UITextFieldDelegate {

    // Top of the storyboard
    @IBOutlet weak var topBar: UIToolbar!
    @IBOutlet weak var shareBtn: UIBarButtonItem!
    @IBOutlet weak var cancelBtn: UIBarButtonItem!

    // Middle of the storyboard
    @IBOutlet weak var topTextField: UITextField!
    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var bottomTextField: UITextField!

    // Bottom of the storyboard
    @IBOutlet weak var bottomBar: UIToolbar!
    @IBOutlet weak var cameraBtn: UIBarButtonItem!
    @IBOutlet weak var albumBtn: UIBarButtonItem!


    // text attributes
    let topTextDefault = "TOP TEXT"
    let bottomTextDefault = "BOTTOM TEXT"
    let noText = ""

    let topTextDelegate = TopTextDelegate()
    let bottomTextDelegate = BottomTextDelegate()

    // meme attributes
    let memeTextAttributes:[String: Any] = [NSStrokeColorAttributeName: UIColor.black, NSForegroundColorAttributeName: UIColor.white, NSFontAttributeName: UIFont( name: "HelveticaNeue-CondensedBlack", size: 40 )!, NSStrokeWidthAttributeName: NSNumber( value: -4.0 )]

    var isKeyboardVisible = false

    // Use of undeclared type 'MemeImage'
    var meme: MemeImage!

    override func viewDidLoad() {
        super.viewDidLoad()

        imageView.contentMode = UIViewContentMode.scaleAspectFit

    }


    override func viewDidAppear(_ animated: Bool) {
        cameraBtn.isEnabled = UIImagePickerController.isSourceTypeAvailable(.camera)
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.subscribeToKeyboardNotifications()
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        unsubscribeFromKeyboardNotifications()
    }

    func initTextFields(textField: UITextField, initialText: String, delegate: UITextFieldDelegate) {

        textField.defaultTextAttributes = memeTextAttributes
        textField.textAlignment = NSTextAlignment.center
        textField.text = initialText
        textField.delegate = delegate

    }

    func keyboardWillShow(_ notification:Notification) {
        if bottomTextField.isFirstResponder {

            // Binary Operator '*' cannot be applied to operands of ttype '()' and 'Int' -----------
            view.frame.origin.y = getKeyboardHeight(notification) * -1


        }
    }

    func keyboardWillHide(_ notification:Notification) {
        if bottomTextField.isFirstResponder {
            view.frame.origin.y = 0.0
        }
    }

    func subscribeToKeyboardNotifications() {
        NotificationCenter.default.addObserver( self, selector: #selector(keyboardWillShow(_:)), name: .UIKeyboardWillShow, object: nil )

        NotificationCenter.default.addObserver( self, selector: #selector(keyboardWillHide(_:)), name: .UIKeyboardWillHide, object: nil )
    }

    func unsubscribeFromKeyboardNotifications() {
        NotificationCenter.default.removeObserver( self, name: .UIKeyboardWillShow, object: nil )
        NotificationCenter.default.removeObserver( self, name: .UIKeyboardWillHide, object: nil )
    }

    func getKeyboardHeight(_ notification:Notification) {
        let userInfo = notification.userInfo
        let keyboardSize = userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue

        // Unexpected non-void return value in void function---------
        return keyboardSize.cgRectValue.height

    }

    func imagePickCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true, completion: nil)
    }

    func imageWasPicked(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
                imageView.contentMode = .scaleAspectFit
                imageView.image = image
                shareBtn.isEnabled = true
                cancelBtn.isEnabled = true

        } else {
            dismiss(animated: true, completion: nil)
        }
    }

    func pickImageType(sourceType: UIImagePickerControllerSourceType) {

        let pickerController = UIImagePickerController()
        pickerController.delegate = self
        pickerController.sourceType = sourceType

        self.present( pickerController, animated: true, completion: nil)

    }

    @IBAction func getImageFromCamera(_ sender: Any) {

        pickImageType(sourceType: UIImagePickerControllerSourceType.camera)

    }

    @IBAction func getImageFromAlbum(_ sender: Any) {

        pickImageType(sourceType: UIImagePickerControllerSourceType.photoLibrary)

    }

    @IBAction func cancel(_ sender: Any) {

        shareBtn.isEnabled = false
        imageView.image = nil
        topTextField.text = topTextDefault
        bottomTextField.text = bottomTextDefault

        meme = nil

    }

    @IBAction func share(_ sender: Any) {

        // Use of local variable 'makeMemedImage' before its declaration ------------
        let memedImage = makeMemedImage()
        let socialController = UIActivityViewController( activityItems: [memedImage], applicationActivities: nil )
        socialController.completionWithItemsHandler = {

            UIActivityType, completion, items, error in

            if completion {

                self.saveMemedImage(memedImage: memedImage) {
                }
                } else {
                    self.dismiss( animated: true, completion: nil )
                }
            self.present(socialController, animated: true, completion: nil)

            }

        func makeMemedImage() -> UIImage {

            topBar.isHidden = true
            bottomBar.isHidden = true

            UIGraphicsBeginImageContext(self.view.frame.size)
            view.drawHierarchy( in: self.view.frame, afterScreenUpdates: true )
            let memedImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
            UIGraphicsEndImageContext()

            topBar.isHidden = false
            bottomBar.isHidden = false

            return memedImage
        }

        func save() {
            self.meme = memedImage.init(topText: self.txtTop.text!, bottomText: self.txtBottom.text!, origImage: self.imgView.image!, memedImage: memedImage)

        }
    }

}

2 个答案:

答案 0 :(得分:0)

主要是错误信息是自我解释...

  

1:使用未声明类型'MemeImage'

你的项目中没有MemeImage类(可能是拼写错误?)

  

2:二进制运算符'*'不能应用于'()'和'Int'类型的操作数

函数没有返回任何内容,这就是为什么你不能在这里使用*(用答案3修复)

  

3:void函数中出现意外的非void返回值

函数声明没有声明的返回值

func getKeyboardHeight(_ notification:Notification) {

更改为Int或您在此处返回的任何内容:

func getKeyboardHeight(_ notification:Notification) -> Int {  
  

4:在声明之前使用局部变量'makeMemedImage'

我发现函数makeMemedImage处于错误的级别

解决方案A (未经推荐 - 但可以解决错误消息)

如果您希望将其作为@IBAction func share(_ sender: Any) {内的函数使用,那么您需要将此块移动到第一次使用

解决方案B (推荐 - 更好的编码风格,只有当您需要一个非常特殊的情况然后内联函数时)

将函数func makeMemedImage(以及其他func save)的嵌套级别更改为@IBAction func share之外。将}func save { ... }之后的行移至 func makeMemedImage`之前的一行

答案 1 :(得分:0)

我只是将它粘贴到一个操场上,然后错误弹出。我猜这个代码是从某些来源复制和粘贴的,几乎没有尝试实际修复它,即,甚至没有所有的括号配对(错误#4)。

  1. 完全意味着它的含义。它无法在项目中的任何位置找到名为MemeImage的类。该课程是否存在?

  2. getKeyboardHeight是一个不会声明其返回类型的函数(这也是错误#3的原因)。所以你试图乘以一个函数。

  3. 参见#2

  4. 您错过了}函数与share函数之间的结束makeMemedImage