在我的课程中声明的属性在尝试在函数内使用时无法识别?

时间:2016-09-05 02:14:51

标签: swift properties declaration

我已检查过该物业的拼写错误,但事实并非如此。我试图使用我在mySong函数中的类中声明的属性parseSongs()

该功能不在课堂内,但它在同一个文件中。并且该类的目标成员资格也设置为项目名称,其他文件也是如此。

我很困惑为什么编译器没有在parseSongs()中识别我的属性名称?

我可以在类之外声明属性,但即使它在类中声明,我也应该可以使用它。

import UIKit

class SongsTableViewController: UITableViewController {

//A property that is an array of type 'Song'
var mySong = [Song]()

private let cache = NSCache()

private func fetchMyData(){

    let myUrl = NSURL(string: "http://itunes.apple.com/search?term=beatles&country=us")!
    let mySession = NSURLSession.sharedSession()

    //The work to be queued initiates
    let myTask = mySession.dataTaskWithURL(myUrl){
    //This closure right here is the Completion Handler

        data, response, error in
        if error != nil{
            //Handle error

        }else{

            let myHttpResponse = response as! NSHTTPURLResponse
            switch myHttpResponse.statusCode {
            case 200..<300:
                print("OK")
                print("data: \(data)")
            default: print("request failed: \(myHttpResponse.statusCode)")

            }
        }
    }
    myTask.resume()
 }

}

func parseJson(myData data: NSData){

do{

    let json: AnyObject? = try NSJSONSerialization.JSONObjectWithData(data, options: [])
    if let unwrappedJson: AnyObject = json{

        parseSongs(unwrappedJson)
    }
}catch{

 }
}


 func parseSongs(json1: AnyObject){
     mySong = []

    //Optional Binding
    if let array = json1["results"] as? [[String:AnyObject]]{
        //For-In loop
        for songDictionary in array{

            if let title = songDictionary["trackName"] as? NSString{

                if let artist = songDictionary["artistName"] as? NSString{

                    if let albumName = songDictionary ["collectionName"] as? NSString{

                        if let artWorkUrl = songDictionary["artWorkUrl100"] as? NSString {
                            let song = Song(artist: (artist as String), title: (title as String), albumName: (albumName as String), artWorkUrl: (artWorkUrl as String))

                            mySong.append(song)
                        }
                    }

                }
            }
        }

        dispatch_async(dispatch_get_main_queue()){
            self.tableView.reloadData()
        }
    }
}

1 个答案:

答案 0 :(得分:0)

要使用在类中声明的属性,必须遵循此

RedisCacheManager

如果你在课外声明它,那么你可以在外部类

的功能中访问它