ios - 请帮助没有意义的协议(protocol)代码
<p><p>我对这句话的一部分感到困惑:</p>
<pre><code>@property(nonatomic, readonly) NSArray <id<NSFetchedResultsSectionInfo>>*sections
</code></pre>
<p>此属性属于 NSFetchedResultsController 类。
虽然 NSFetchedResultsSectionInfo 是一个协议(protocol),但 <code>id<NSFetchedResultsSectionInfo></code> 是一个必须符合 NSFetchedResultsSectionInfo 协议(protocol)的对象类型。 <code>id<NSFetchedResultsSectionInfo></code> 不是协议(protocol),因此将它们括在尖括号中
<code><id<NSFetchedResultsSectionInfo>></code> 没有意义。
语句 NSArray <code><id<NSFetchedResultsSectionInfo>></code> 对我来说没有意义,因为你怎么能让对象类型 id 成为 NSArray 类的协议(protocol)。有人可以对此有所了解吗?谢谢。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>该语法实际上并不意味着数组符合任何协议(protocol)。该符号具有误导性。它只告诉你 <em>inside</em> 数组的值是 <code>id</code> 并且符合 <code>NSFetchedResultsSectionInfo</code> 协议(protocol)。</p>
<p><a href="https://developer.apple.com/library/prerelease/ios/documentation/CoreData/Reference/NSFetchedResultsController_Class/index.html#//apple_ref/occ/instp/NSFetchedResultsController/sections" rel="noreferrer noopener nofollow">documentation</a>状态:</p>
<blockquote>
<p>The objects in the sections array implement the NSFetchedResultsSectionInfo protocol.</p>
</blockquote>
<p>您需要确保它只包含指定类型的值,并且如果您这样做,编译器会告诉您您正在尝试插入不兼容的对象。</p>
<p>您可能想看看 <a href="https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithProtocols/WorkingwithProtocols.html" rel="noreferrer noopener nofollow">apple doc</a> 的最后一部分.</p>
<blockquote>
<p>If you’re writing an iOS app that uses the Core Data framework, for example, you’ll likely run into the NSFetchedResultsController class. This class is designed to help a data source object supply stored data to an iOS UITableView, making it easy to provide information like the number of rows.</p>
<p>If you’re working with a table view whose content is split into multiple sections, you can also ask a fetched results controller for the relevant section information. Rather than returning a specific class containing this section information, the NSFetchedResultsController class instead returns an anonymous object, which conforms to the NSFetchedResultsSectionInfo protocol. This means it’s still possible to query the object for the information you need, such as the number of rows in a section</p>
<p>Even though you don’t know the class of the sectionInfo object, the NSFetchedResultsSectionInfo protocol dictates that it can respond to the numberOfObjects message.</p>
</blockquote></p>
<p style="font-size: 20px;">关于ios - 请帮助没有意义的协议(protocol)代码,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/30873650/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/30873650/
</a>
</p>
页:
[1]