如何使用objective-c

时间:2016-02-07 15:44:26

标签: ios objective-c cloudkit

我在CloudKit中有一个记录类型。它存储一个字符串字段和一个资产字段。使用仪表板我将图像上传到资产字段。

我可以检索字符串字段。我现在需要检索资产字段的内容并使用objective-c将其存储到图像中。有人可以帮忙吗?

我正在尝试在表格视图中显示记录类型。我有一个名为TJKCategoriesViewController的类,它继承自UIViewController。

我在TJKCategoriesViewController类的viewDidLoad方法中有这个代码:

// get all categories
_jokeCategories = [[NSMutableArray alloc] init];
CKDatabase *jokePublicDatabase = [[CKContainer containerWithIdentifier:JOKE_CONTAINER] publicCloudDatabase];
NSPredicate *predicateCategory = [NSPredicate predicateWithFormat:@"CategoryName != %@", CATEGORY_TO_REMOVE_OTHER];
CKQuery *queryCategory = [[CKQuery alloc] initWithRecordType:CATEGORY_RECORD_TYPE predicate:predicateCategory];
NSSortDescriptor *sortCategory = [[NSSortDescriptor alloc] initWithKey:CATEGORY_FIELD_NAME ascending:YES];
queryCategory.sortDescriptors = [NSArray arrayWithObjects:sortCategory, nil];
[jokePublicDatabase performQuery:queryCategory inZoneWithID:nil completionHandler:^(NSArray<CKRecord*>* results, NSError * error)
 {
     if (!error)
     { 
         // add all categories to array
         for (CKRecord* jokeCategory in results)
         {
             TJKCategories *categories = [TJKCategories initWithCategory:[jokeCategory valueForKey:CATEGORY_FIELD_NAME] categoryImage:[jokeCategory valueForKey:@"CategoryImage"]];
             [_jokeCategories addObject:categories];
         }


         NSLog(@"Categories = %@",_jokeCategories);

         // setup table
         [self setupTableContents];
     }
     else
     {
         // display alert message and pop the view controller from the stack
         GHSAlerts *alert = [[GHSAlerts alloc] initWithViewController:self];
         errorActionBlock errorBlock = ^void(UIAlertAction *action) {[self closeCategories:self];};
         [alert displayErrorMessage:@"Oops!" errorMessage:@"The joke categories failed to load. This screen will close. Just try again!" errorAction:errorBlock];
     }
 }];

这是方法“setupTableContents”

// setup the array that will hold the table's contents
-(void)setupTableContents
{   
    // store to property and reload the table contents
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.categoriesTableView reloadData];
    });
}

TJKCategories是一个存储记录类型中两个字段的类。这是TJKCategories.h文件:

#import <UIKit/UIKit.h>

@interface TJKCategories : NSObject

#pragma Properties

@property (nonatomic, copy) UIImage *categoryImage;
@property (nonatomic, copy) NSString *categoryName;

#pragma Initializers
+(instancetype)initWithCategory:(NSString *)categoryName
                  categoryImage:(UIImage *)categoryImage;


-(instancetype)initWithCategory:(NSString *)categoryName
                  categoryImage:(UIImage *)categoryImage;

@end

这是TJKCategories.m档案:

// designated initializer
-(instancetype)initWithCategory:(NSString *)categoryName categoryImage:(UIImage *)categoryImage
{
    self = [super init];

    if (self)
    {
        self.categoryImage = categoryImage;
        self.categoryName = categoryName;
    }

    return self;
}

// class level initializer
+(instancetype)initWithCategory:(NSString *)categoryName categoryImage:(UIImage *)categoryImage
{
    TJKCategories *category = [[self alloc]initWithCategory:categoryName categoryImage:categoryImage];
    return category;
}

// override default initializer
-(instancetype)init
{
    return [self initWithCategory:@"" categoryImage:nil];
}

在我使用的viewDidLoad方法中:[jokePublicDatabase performQuery:queryCategory inZoneWithID:nil completionHandler:^(NSArray * results,NSError * error)

检索记录类型的结果。然后我循环遍历结果数组,我可以检索字符串“CATEGORY_FOR_NAME”(这是“CategoryName”的常量,但是当我尝试检索图像“CategoryImage”的字段时,我得到并且“无法识别的选择器发送到实例“错误。

如果您需要知道常量背后的名称是我的联系人文件:

// declare constants
extern NSString *const DEFAULT_CATEGORY;
extern NSString *const JOKE_CONTAINER;
extern NSString *const CATEGORY_RECORD_TYPE;
extern NSString *const CATEGORY_FIELD_NAME;
extern NSString *const CATEGORY_TO_REMOVE_OTHER;
extern NSString *const CATEGORY_TO_REMOVE_RANDOM;
extern NSString *const JOKE_RECORD_TYPE;
extern NSString *const JOKE_DESCR;
extern NSString *const JOKE_SUBMITTED_BY;
extern NSString *const JOKE_TITLE;

// assign values to constants
NSString *const DEFAULT_CATEGORY = @"None";
NSString *const JOKE_CONTAINER = @"iCloud.com.glennseplowitz.Jokes";
NSString *const CATEGORY_RECORD_TYPE = @"Categories";
NSString *const CATEGORY_FIELD_NAME = @"CategoryName";
NSString *const CATEGORY_TO_REMOVE_OTHER = @"Other";
NSString *const CATEGORY_TO_REMOVE_RANDOM = @"Random";
NSString *const JOKE_RECORD_TYPE = @"Jokes";
NSString *const JOKE_DESCR = @"jokeDescr";
NSString *const JOKE_SUBMITTED_BY = @"jokeSubmittedBy";
NSString *const JOKE_TITLE = @"jokeTitle";

所以我要做的是从记录类型中检索资产类型“CategoryImage”并将其存储到UIImage。但就像我说它给我的错误“无法识别的选择器发送到实例”因为我认为它无法直接将资产字段转换为UIImage字段。

谢谢, 格伦

1 个答案:

答案 0 :(得分:1)

想出来!我改变了我的TJKCategories类来接受CKAsset而不是UIImage。然后我将CKAsset转换为UIImage。

这是TJKCategories.h文件:

#import <UIKit/UIKit.h>
#import <CloudKit/CloudKit.h>

@interface TJKCategories : NSObject

#pragma Properties

@property (nonatomic, copy) UIImage *categoryImage;
@property (nonatomic, copy) NSString *categoryName;

#pragma Initializers
+(instancetype)initWithCategory:(NSString *)categoryName
                  categoryImage:(CKAsset *)categoryImageAsset;


-(instancetype)initWithCategory:(NSString *)categoryName
                  categoryImage:(CKAsset *)categoryImageAsset;


@end

这是TJKCategories.m文件:

#import "TJKCategories.h"

@interface TJKCategories()

#pragma Properities
@property (nonatomic, strong) CKAsset *categoryImageAsset;

@end

@implementation TJKCategories

#pragma initializers

// designated initializer
-(instancetype)initWithCategory:(NSString *)categoryName categoryImage:(CKAsset *)categoryImageAsset
{
    self = [super init];

    if (self)
    {
        self.categoryImageAsset = categoryImageAsset;
        self.categoryName = categoryName;
        UIImage *image = [[UIImage alloc] initWithContentsOfFile:self.categoryImageAsset.fileURL.path];
        self.categoryImage = image;       
    }

    return self;
}

// class level initializer
+(instancetype)initWithCategory:(NSString *)categoryName categoryImage:(CKAsset *)categoryImageAsset
{
    TJKCategories *category = [[self alloc]initWithCategory:categoryName categoryImage:categoryImageAsset];
    return category;
}

// override default initializer
-(instancetype)init
{
    return [self initWithCategory:@"" categoryImage:nil];
}

我也换了一行:

TJKCategories *categories = [TJKCategories initWithCategory:[jokeCategory valueForKey:CATEGORY_FIELD_NAME] categoryImage:[jokeCategory **objectForKey**:@"CategoryImage"]];

TJKCategories *categories = [TJKCategories initWithCategory:[jokeCategory valueForKey:CATEGORY_FIELD_NAME] categoryImage:[jokeCategory **valueForKey**:@"CategoryImage"]];

请注意,CategoryImage现在使用valueForKey而不是objectForKey。