开发者

"out of scope" error when iterating an NSMutableArray

Why am I getting an "out of scope" error whenever I try to access the "url" variable in this loop?

for(NSString *url in self.winnerImageURLs) {
        [mediaItemStri开发者_运维知识库ng appendFormat:@"{\"type\":\"image\",\"src\":\"%@\",\"href\":\"%@\"},", url, url];
    }

The class of very item in the "self.winnerImageURLs" NSMutableArray comes back as NSCFString so I'm not sure what the dealio is here.

Any ideas on what I'm doing wrong???

Thanks


If you're talking about accessing the url variable in the debugger, it happens quite a lot. It's not a problem in itself.


Short anwer is that it is supposed to be like that. A longer answer is that NSString is a class cluster.

Class cluster

A class cluster is an architecture that groups a number of private, concrete subclasses under a public, abstract superclass. The grouping of classes in this way provides a simplified interface to the user, who sees only the publicly visible architecture. Behind the scenes, though, the abstract class is calling up the private subclass most suited for performing a particular task.

Many common Cocoa classes are implemented as class clusters, including NSArray, NSString, and NSDictionary.

You create and interact with instances of the cluster just as you would any other class. Behind the scenes, though, when you create an instance of the public class, the class returns an object of the appropriate subclass based on the creation method that you invoke. (You don’t, and can’t, choose the actual class of the instance.)

NSString example

NSString *a = @"UTF32.txt";

NSString *b = [NSHomeDirectory() stringByAppendingPathComponent:a];

NSTextStorage *storage = [[NSTextStorage alloc] initWithString:b];

NSString *c = [storage string];

Each of a,b and c may be (and in 10.5 is) an instance of a different private subclass (and in fact, on Mac OS X v10.5, each is). Although each of the objects is of a private subclass of NSString, it’s convenient to consider each of the objects to be instances of the NSString class.

About Class Clusters in Apple Developer site: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html#//apple_ref/doc/uid/TP40002974-CH4-SW34

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜