我有一些代码必须保留为c ++,但我需要在这些c ++类中存储objective-c对象.当它们存储在这里时,它们不会被其他任何地方引用,所以我不能将它们从我的下面删除.在ARC之前,我只是在将它们放入c ++类之前保留它们,并在它们被删除时自动释放它们.一切都有罚款.
但是对于ARC,我不知道该怎么做.是否使c ++变量__unsafe_un得足够?似乎不是因为一旦obj-c代码不再使用那些对象它将被删除,或者我不理解__unsafe_unretained做了什么.我可以在ARC下调用CFRetain()和CFAutorelase()吗?
在ARC下处理这个问题的正确方法是什么?NSArray做了什么深入了解它所存储的对象?
我有一个ARC项目,我正在尝试绘制垂直线性渐变.下面的代码适用于模拟器,但EXC_BAD_ACCESS在设备上进行测试时会抛出内存/ 错误.该应用程序使用以下两行代码崩溃:
NSArray *colorArray = [NSArray arrayWithObjects:(__bridge id)topColor, (__bridge id)bottomColor, nil];
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colorArray, colorLocations);
Run Code Online (Sandbox Code Playgroud)
这两行代码取自以下代码(供参考):
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
[self createGradientForContext:context andView:self.captionView];
[self createGradientForContext:context andView:self.linksView];
[self createGradientForContext:context andView:self.commentView];
}
- (void)createGradientForContext:(CGContextRef)context andView:(UIView *)view
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat colorLocations[] = { 0.0f, 1.0f };
CGColorRef topColor = [[UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1.0f] CGColor];
CGColorRef bottomColor = [[UIColor colorWithRed:48.0f/255.0f green:48.0f/255.0f blue:48.0f/255.0f alpha:1.0f] CGColor];
NSArray *colorArray = [NSArray arrayWithObjects:(__bridge id)topColor, (__bridge …Run Code Online (Sandbox Code Playgroud) core-graphics objective-c core-foundation ios automatic-ref-counting
我正在尝试解析xml文件并且没有错误,但在尝试读取它时,解析器:didStartElement事件未被触发.我做错了什么?感谢帮助.
- (void)viewDidLoad
{
[super viewDidLoad];
// xml connect
NSURL *url = [[NSURL alloc] initWithString:@"http://www.test.com/list.xml"];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
//Start parsing the XML file.
BOOL success = [xmlParser parse];
if(success)
NSLog(@"No Errors");
else
NSLog(@"Error!");
}
// reading xml...
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:@"main"]) {
//Initialize the array.
apps = [[NSMutableArray alloc] init];
}
else if([elementName isEqualToString:@"prog"]) {
//Extract the attribute here.
idUsuari = [attributeDict objectForKey:@"Id"];
NSLog(@"ID: %@", idUser);
} …Run Code Online (Sandbox Code Playgroud)