例如,如果我们有一个Foo继承自 class 的类Bar,并且符合协议Baz:
class Bar {}
protocol Baz {}
Run Code Online (Sandbox Code Playgroud)
class Foo: Bar, Baz {}
如果我们事先不知道这Bar是一个类并且Baz是一个协议(例如,当阅读别人的代码时)怎么办?在这种情况下,由于 后面有多个声明,情况可能会class Foo:很清楚。在 Swift 中,协议一致性声明必须位于继承子句中的超类声明之后。另一方面,这也可能是一个没有继承且符合多个协议的类。
另外,如果继承子句中只有一个声明怎么办?
class Foo: Bar {}
Run Code Online (Sandbox Code Playgroud)
或者:
class Foo: Baz {}
Run Code Online (Sandbox Code Playgroud)
在 Objective-C 中,由于协议名称被尖括号括起来,所以很清楚。在 Swift 中,尖括号用于泛型。对于这种情况,是否有语法支持使协议一致性在视觉上更加明确?
我希望在用户的库中获取一个MPMediaItemCollection或NSArray所有艺术家.这是我当前的代码(显然不起作用):
- (void)viewDidLoad
{
[super viewDidLoad];
MPMediaQuery *artistsQuery = [MPMediaQuery artistsQuery];
self.artistsArray = artistsQuery.collections;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.artistsArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ArtistsCell"];
cell.textLabel.text = [(MPMediaItemCollection *)self.artistsArray[indexPath.row] valueForProperty:MPMediaPlaylistPropertyName];
NSLog(@"%@", cell.textLabel.text);
return cell;
}
Run Code Online (Sandbox Code Playgroud)