NSMutableArray导致崩溃

时间:2018-03-02 20:08:00

标签: xcode nsmutablearray

对于这篇长篇文章我感到很遗憾,但是我在我的智慧结束时已经被困了好几天了。这是场景。我的应用程序从核心数据加载响应,将值转换为NSStrings,以便我可以将它们添加到NSDictionary。然后将NSDictionary转换为NSData,以便将其作为文件附加到电子邮件中。这样做的目的是,我可以创建一个信息数据库,包括图像,视频等。我能够让一切工作,除了我有一个NSMutableArray的问题。这是过程:

我创建了一个事件,然后使用此代码加载要导出的数据。

EventDB *per = [[EventDB alloc]init];
per.customLayoutArray = [record.customLayoutArray description] ?
[record.customLayoutArray description] : @"";
NSDictionary *dict = [per dictionaryWithValuesForKeys:@[@"customLayoutArray"];
NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:0 error:NULL];

然后我使用MFMailComposer通过电子邮件发送数据。然后我有一个自定义UTI,允许我打开电子邮件中的URL,然后导入数据并将其加载到我的coredata实体中

if([[url pathExtension] isEqualToString:@"ipix"]) {
   NSData *data = [NSData dataWithContentsOfURL:url];
   NSError *error;
    NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:data
                                                                 options:kNilOptions
                                                                   error:&error];


    NSEntityDescription *entity = [NSEntityDescription entityForName:@"TSPItem" 
   inManagedObjectContext:self.managedObjectContext];
    TSPItem *record = (TSPItem *)[[NSManagedObject alloc] initWithEntity:entity
   insertIntoManagedObjectContext:self.managedObjectContext];

    if (record) {

   NSString *datetime = [jsonData objectForKey:@"customLayoutArray"];
        record.customLayoutArray = [[datetime propertyList] mutableCopy];
     }

工作正常。它确实以我想要的方式导入,但是当我启动事件时,我收到此崩溃消息

** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 
'-[__NSCFString apply]: unrecognized selector sent to instance 0x1c81a5f60

现在这里是崩溃的代码。

 NSMutableArray *archiveArray = self.record.customLayoutArray;
NSString *mycustom = [NSString stringWithFormat:@"%@_customlayout", 
                                            self.record.eventname];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:archiveArray];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:mycustom];
self.customLayoutArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];


NSLog(@"BOOTH EVENT ID %@", self.customLayoutArray);
[self.customLayoutArray makeObjectsPerformSelector:@selector(apply)];

这是来自BOOTH EVENT ID的日志

 BOOTH EVENT ID (
"<Rectangle:0x102d38cb0self.scaleValue=1.842393\n, self.rotateValue=0.000000\n, self.width=368.478516\n, self.height=368.478516\n, self.radius=0\n, self.frame={{104, 113.5}, {200, 200}}\n, self.isApplied=NO\n>",
"<Rectangle:0x102d393c0self.scaleValue=1.000000\n, self.rotateValue=0.000000\n, self.width=200.000000\n, self.height=200.000000\n, self.radius=0\n, self.frame={{253, 273.5}, {200, 200}}\n, self.isApplied=NO\n>"
)

应用程序崩溃了。现在,如果我在我的iPad上加载原始事件(我没有导出的那个),那么应用程序就可以完美运行,并且BOOTH EVENT ID的NSLog响应是相同的。

&#34;申请&#34; section指的是这个文件。

#import "Rectangle.h"
#import "DeviceSize.h"

@implementation Rectangle

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
    // Initialization code
    self.clipsToBounds = YES;
    self.userInteractionEnabled = YES;


}
return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeObject:[NSNumber numberWithFloat:self.scaleValue] forKey:@"ScaleValue"];
[coder encodeObject:[NSNumber numberWithFloat:self.rotateValue] forKey:@"RotateValue"];

[coder encodeObject:[NSNumber numberWithFloat:self.width] forKey:@"Width"];
[coder encodeObject:[NSNumber numberWithFloat:self.height] forKey:@"Height"];

[coder encodeObject:[NSNumber numberWithInteger:self.radius] forKey:@"Radius"];
[coder encodeObject:[NSNumber numberWithBool:self.isApplied] forKey:@"isApplied"];

[coder encodeObject:self.image forKey:@"Image"];
[coder encodeObject:self.backgroundColor forKey:@"BackgroundColor"];
[coder encodeObject:[NSValue valueWithCGPoint:self.center]    forKey:@"CenterPoint"];
}

- (id)initWithCoder:(NSCoder *)coder {
self = [super init];
if (self) {
    self.scaleValue = [[coder decodeObjectForKey:@"ScaleValue"]  floatValue];
    self.rotateValue = [[coder decodeObjectForKey:@"RotateValue"] floatValue];

    self.width = [[coder decodeObjectForKey:@"Width"] floatValue];
    self.height = [[coder decodeObjectForKey:@"Height"] floatValue];

    self.radius = [[coder decodeObjectForKey:@"Radius"] integerValue];
    self.isApplied = [[coder decodeObjectForKey:@"isApplied"] boolValue];

    [self.layer setCornerRadius:self.radius];
    self.image = [coder decodeObjectForKey:@"Image"];
    [self setBackgroundColor:[coder decodeObjectForKey:@"BackgroundColor"]];
    //


    if (self.width == self.height)
    {
        CGRect rect = CGRectMake(0, 0,200, 200);
        self.frame = rect;

    }

    if (self.width > self.height)
    {
        CGRect rect = CGRectMake(0, 0,200, 150);
        self.frame = rect;

    }

    if (self.width < self.height)
    {
        CGRect rect = CGRectMake(0, 0,150, 200);
        self.frame = rect;

    }


    self.center = [[coder decodeObjectForKey:@"CenterPoint"] CGPointValue];


}
return self;
}


- (void)drawRect:(CGRect)rect
{
/* Set UIView Border */
// Get the contextRef
CGContextRef contextRef = UIGraphicsGetCurrentContext();

// Set the border width
CGContextSetLineWidth(contextRef, 5.0);

// Set the border color to RED
CGContextSetRGBStrokeColor(contextRef, 255.0, 0.0, 0.0, 1.0);

// Draw the border along the view edge
CGContextStrokeRect(contextRef, rect);
}

- (BOOL)canBecomeFirstResponder {
return YES;
}

- (NSString *)description {
NSMutableString *description = [NSMutableString stringWithFormat:@"<%@:%p", NSStringFromClass([self class]), self];

[description appendFormat:@"self.scaleValue=%f\n", self.scaleValue];
[description appendFormat:@", self.rotateValue=%f\n", self.rotateValue];
[description appendFormat:@", self.width=%f\n", self.width];
[description appendFormat:@", self.height=%f\n", self.height];
[description appendFormat:@", self.radius=%li\n", (long)self.radius];
[description appendFormat:@", self.frame=%@\n", NSStringFromCGRect(self.frame)];
[description appendFormat:@", self.isApplied=%@\n", self.isApplied ? @"YES" : @"NO"];
[description appendString:@">"];
return description;
}

- (id)copyWithZone:(NSZone *)zone {
Rectangle *copy = [[[self class] allocWithZone:zone] init];

if (copy != nil) {
    copy.scaleValue = self.scaleValue;
    copy.rotateValue = self.rotateValue;
    copy.width = self.width;
    copy.height = self.height;
    copy.radius = self.radius;
    copy.frame = self.frame;
    copy.isApplied = self.isApplied;
}

return copy;
}


@end


@implementation Rectangle(ApplyRotate)

#pragma mark -
- (Rectangle *)apply {
if (self.isApplied) {
    return self;
}

Rectangle *rectangle = self;
CGPoint centerPoint = rectangle.center;

CGAffineTransform rotate = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(rectangle.rotateValue));
CGAffineTransform scale = CGAffineTransformMakeScale(rectangle.scaleValue, rectangle.scaleValue);
CGAffineTransform scaleAndRotate = CGAffineTransformConcat(rotate, scale);
rectangle.transform = scaleAndRotate;

rectangle.center = centerPoint;
rectangle.isApplied = YES;

return rectangle;
}

@end

0 个答案:

没有答案