即时编写应用程序,但无法弄清楚如何将此特定的Objective-C行转换为swift。任何帮助都感激不尽。我已经能够翻译其他行很好,但这一个抛出一些异常错误。这是原始的Objective-C行:应用程序只显示一个单元格(一首歌曲)而不是所有的单元格
MPMediaItem * rowItem = [songs objectAtIndex:indexPath.row];
我一个小白和已经尝试了许多变化,包括:
变种rowItem = songs.objectAtIndex(indexPath.row)作为MPMediaItem
和
var rowItem:MPMediaItem = songs.objectAtIndex(indexPath.row)as MPMediaItem
但我一直在这么长时间,我看不到它应该是什么。非常感谢!
编辑:
歌曲被定义为:
让songsQuery = MPMediaQuery.songsQuery()
让歌曲= [songsQuery.items]如NSArray的
我刚接收一个exe_breakpoint错误。如果我使用...作为[MPMediaItem]将MPMediaItem封装在数组中,但我无法获得属性值,例如MPMediaItemPropertyTitle和MPMediaItemPropertyArtist。
更新: 通过使用for循环获取MPMediaItemPropertyTitle和MPMediaPropertyArtist。 “歌曲”变量的定义如上所述。 @Antonio,删除括号不起作用。这是我当前的代码:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
// Configure the cell...
let songsQuery = MPMediaQuery.songsQuery()
let allSongsArray = [songsQuery.items] as NSArray
var singleItem: AnyObject = allSongsArray[indexPath.row]
var rowItem = singleItem as [MPMediaItem]
if rowItem.count > 0 {
cell.textLabel?.text = rowItem[0].valueForProperty(MPMediaItemPropertyTitle) as NSString
cell.detailTextLabel?.text = rowItem[0].valueForProperty(MPMediaItemPropertyArtist) as NSString
}
return cell
}
的问题,我现在是当我测试的应用程序,它仅显示非常的第一首歌曲在我的图书馆,并像它应该不会列出每首歌曲。感谢您提供任何帮助!
更新2:
得到它用下面的代码工作:
class SongsViewController: UITableViewController {
var allSongs : [MPMediaItem] = []
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
allSongs += MPMediaQuery.songsQuery().items as [MPMediaItem]
println(allSongs.count)
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return allSongs.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
// Configure the cell...
var rowItem = allSongs[indexPath.row]
cell.textLabel?.text = rowItem.valueForProperty(MPMediaItemPropertyTitle) as NSString
cell.detailTextLabel?.text = rowItem.valueForProperty(MPMediaItemPropertyArtist) as NSString
return cell
}
你应该提供更多的细节 - 如何定义'songs'?你有什么样的错误?编译或运行时?什么是错误信息? – Antonio 2015-02-10 17:41:14
@Antonio感谢您的回复,我编辑了原帖。 – air6199 2015-02-10 17:46:59
为什么那些方括号'[songsQuery.items]'? items属性的数据类型是什么? – Shoaib 2015-02-10 17:53:12