我刚刚开始使用Core Data,作为一个学习练习,我正在构建一个应用程序,我需要在单个表视图中显示不同类型的对象.
举个例子,假设我有一个"奶酪"的实体和一个与"海盗"无关的实体.在我的应用程序的主屏幕,用户应该能够创建无论是"奶酪"或"海盗"的实例添加到表视图.
因此,使用核心数据编辑器我已经为Cheese和Pirate创建了实体......但是,NSFetchRequest似乎只允许您一次检索一种类型的实体:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Cheese" inManagedObjectContext:_context];
[fetchRequest setEntity:entity];
Run Code Online (Sandbox Code Playgroud)
有没有办法执行检索所有"奶酪"和"海盗"对象的提取?
谢谢.
我正在尝试将使用Core Data的项目从Objective-C转换为Swift.
数据模型的结构使我有一个包含其他文件夹的主文件夹 - 这些文件夹也可以通过"parentFolder"关系包含其他文件夹.
目前,我在Objective-C中执行此操作以检索主文件夹(它找到唯一没有"parentFolder"的文件夹,并按预期工作):
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:"Folder"];
request.predicate = [NSPredicate predicateWithFormat:@"parentFolder == %@", nil];
Run Code Online (Sandbox Code Playgroud)
在转换为Swift时,我想做同样的事情:
let request = NSFetchRequest(entityName: "Folder")
request.predicate = NSPredicate(format: "parentFolder == %@", nil)
Run Code Online (Sandbox Code Playgroud)
...但编译器抱怨"缺少参数标签'argumentArray:'在调用中.(我似乎在混淆它认为我需要使用NSPredicate(格式:argumentArray :)方法代替......)
在Swift中有没有正确的方法呢?
在iOS 8中,我正在尝试添加UIImageView作为UITextView的子视图,类似于此处所示 - 但是图像下方的文本.

我想使用排除路径来执行此操作,因为在其他设备上,我可能会根据屏幕大小对图像进行不同的定位.
但是有一个问题,如果用于创建排除路径的CGRect的Y原点为0,并占用textView的整个宽度,则排除失败并且文本显示在排除路径中(因此文本显示在后面imageView,你可以在截图中看到).
为了测试这个,我使用"单视图"Xcode模板构建了一个简单的应用程序,具有以下内容:
- (void)viewDidLoad {
[super viewDidLoad];
// set up the textView
CGRect frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
UITextView *textView = [[UITextView alloc] initWithFrame:frame];
[textView setFont:[UIFont systemFontOfSize:36.0]];
[self.view addSubview:textView];
textView.text = @"I wish this text appeared below the exclusion rect and not within it.";
// add the photo
CGFloat textViewWidth = textView.frame.size.width;
// uncomment if you want to see that the exclusion path DOES work when not taking up the full width:
// textViewWidth …Run Code Online (Sandbox Code Playgroud) 我的应用程序的一部分,具有图片浏览器,有点类似于苹果的照片应用程序,最初的视图控制器来浏览照片缩略图,当你在照片上轻按显示真实的细节图.
我使用ALAssetsLibrary访问的照片,和我通过的ALAsset URL的阵列,以我的详细信息视图控制器,这样你就可以从一张照片到下一个刷卡.
一切都很好,直到我从一张照片到另一张照片(在详细视图控制器中)滑动时收到ALAssetsLibraryChangedNotification,这通常会导致崩溃:
通知:资产库在发生通知时更改了我自己的NSLog
在我开始在缩略图浏览器中重新加载资源时加载资产... //我自己的NSLog
断言失败:(size == bytesRead),function - [ALAssetRepresentation _imageData],file /SourceCache/AssetsLibrary/MobileSlideShow-1373.58.1/Sources/ALAssetRepresentation.m,第224行.
它崩溃的特定代码行是调用[currentRep metadata],如下所示:
- (void)someMethod {
NSURL *assetURL = [self.assetURLsArray objectAtIndex:index];
ALAsset *currentAsset;
[self.assetsLibrary assetForURL:assetURL resultBlock:^(ALAsset *asset) {
[self performSelectorInBackground:@selector(configureDetailViewForAsset:) withObject:asset];
} failureBlock:^(NSError *error) {
NSLog(@"failed to retrieve asset: %@", error);
}];
}
- (void)configureDetailViewForAsset:(ALAsset *)currentAsset {
ALAssetRepresentation *currentRep = [currentAsset defaultRepresentation];
if (currentAsset != nil) {
// do some stuff
}
else {
NSLog(@"ERROR: currentAsset is nil");
}
NSDictionary *metaDictionary;
if (currentRep != nil) {
metaDictionary …Run Code Online (Sandbox Code Playgroud) 我有一个基于核心数据的应用程序,它有一个对象(列表)到许多对象(列表项)关系.我正在设备之间同步数据,作为其中一部分,我从后台线程中的XML文件导入列表(通过NSOperation子类).
当我更新现有列表时,我删除了所有旧列表项(来自特定于该线程的NSManagedObjectContext)并将其替换为XML文件中的新列表...删除是通过枚举项目来处理的列表:
for (ListItemCD *item in listToUpdate.listItems) {
[self.importContext deleteObject:item];
}
Run Code Online (Sandbox Code Playgroud)
但是,有一段时间,我在枚举期间遇到了崩溃:
*由于未捕获的异常'NSGenericException'而终止应用程序,原因:'* Collection <_NSFaultingMutableSet:0x4fcfcb0>在枚举时发生了变异.
我不知道从哪里开始寻找问题的原因.在枚举发生时,我不会修改代码的任何其他部分中的列表.可以同时存在多个线程,因为导入/更新了不同的列表...将在另一个线程中保存上下文导致问题 - 因为它还通知主要上下文(如果它与枚举同时发生) ?
如果它有帮助,这里是来自我的NSOperation子类的"main"函数的代码(我从Core Data中删除旧的列表项,并通过解析XML数据来更新列表):
- (void)main {
// input the xml data into GDataXML
NSData *xmlData = [[NSMutableData alloc] initWithContentsOfFile:self.filePath];
NSError *error;
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];
// get the list name (so that I know which list to update)
NSString *listName;
NSArray *listNames = [doc.rootElement elementsForName:@"listName"];
if (listNames.count > 0) {
GDataXMLElement *listNameElement = (GDataXMLElement *) [listNames objectAtIndex:0];
listName …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个子类UITableViewCell,我在右上角绘制一个图像.我有完美的工作 - 除非我设置self.backgroundView,我的背景图像掩盖了drawRect中绘制的图像.
必须有一种方法可以设置背景图像(和selectedBackgroundView),而不会掩盖drawRect中正在进行的操作.
我是以错误的方式来做这件事的吗?
编辑:我已经发布了一个问题示例项目.
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
// TODO: figure out why this covers up self.starImage that's drawn in drawRect
self.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellBackground.png"]] autorelease];
}
return self;
}
- (void)drawRect:(CGRect)rect {
[self.starImage drawAtPoint:CGPointMake(self.bounds.size.width - self.starImage.size.width, 0.0)];
}
Run Code Online (Sandbox Code Playgroud)
编辑2:在AWrightIV的要求下,这是我如何工作...这根本不需要子类化UITableViewCell.我只是在cell.backgroundView中添加一个子视图:
// create a UIImageView that contains the background image for the cell
UIImageView *bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellBackground.png"]];
// create another UIImageView that contains the …Run Code Online (Sandbox Code Playgroud) 我正在使用CICrop通过切断图像的顶部和底部将图像裁剪为特定大小.
然后,我应用类似CIMultiplyCompositing过滤器的东西,将裁剪后的图像与另一个图像组合在一起.
两个图像的大小相同,但结果显示两个图像不对齐...一个是偏移的.
所以,我检查了以下内容:
NSLog(@"image after crop: %g, %g, %g, %g", imageToFilter.extent.origin.x,
imageToFilter.extent.origin.y,
imageToFilter.extent.size.width,
imageToFilter.extent.size.height);
NSLog(@"second image: %g, %g, %g, %g", secondImage.extent.origin.x,
secondImage.extent.origin.y,
secondImage.extent.size.width,
secondImage.extent.size.height);
Run Code Online (Sandbox Code Playgroud)
这表明裁剪图像的origin.y具有我看到的偏移(使用CICrop的结果):
image after crop: 0, 136, 3264, 2176
second image: 0, 0, 3264, 2176
Run Code Online (Sandbox Code Playgroud)
那么,有没有办法让我重置裁剪后的图像"extent"rect,以便origin.y为零?检查CIImage的文档,"extent"是一个只读属性.
或者我将不得不做一些非常低效的转换到另一个图像类型/原始数据,然后回到CIImage?
谢谢你的建议.
我有一个使用tableview的应用程序,以及我作为子视图添加到每个自定义单元格的UIButton,如下所示:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
checkButton = [[UIButton buttonWithType:UIButtonTypeCustom] initWithFrame:CGRectMake(2.0, 2.0, 40.0, 40.0)];
[cell.contentView addSubview:checkButton];
// lot's of other code
return cell;
}
Run Code Online (Sandbox Code Playgroud)
我认为一切都很好,直到我开始使用Instruments来确保我没有任何内存泄漏,但我发现将UIButton添加为单元格的子视图就像在某种程度上导致UIKit内部泄漏.
具体来说,我得到每个单元格行的内存泄漏(每次按钮被添加为子视图),泄漏的对象是"CALayer",负责框架为" - [UIView _createLayerWithFrame:]".
我在这里做错了吗?
在XCode 4中使用分析器,由于设置了这样的属性,我收到了潜在内存泄漏的警告:
self.newDog.dogName = self.dogNameTextField.text;
Run Code Online (Sandbox Code Playgroud)
具体警告是:
属性返回一个具有+1保留计数(拥有引用)的Objective-C对象.
在第513行分配的对象稍后在此执行路径中未引用,并且保留计数为+1(对象泄漏)
如果我没有使用self设置属性,则警告消失......但我不确定这是否会导致其他问题,因为我读过的所有内容基本上都说在设置/获取属性时始终使用self:
newDog.dogName = self.dogNameTextField.text;
Run Code Online (Sandbox Code Playgroud)
我在这里做错了吗?以下是视图控制器中发生警告的一些精简代码:
@interface AddDogViewController : UITableViewController {
Dog *newDog;
}
@property (nonatomic, retain) Dog *newDog;
@implementation AddDogViewController
@synthesize newDog;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// some other code
self.newDog.dogName = self.dogNameTextField.text;
// some other code
}
- (void)dealloc {
[newDog release];
[super dealloc];
}
@end
Run Code Online (Sandbox Code Playgroud) ios ×6
iphone ×5
core-data ×3
uitableview ×2
alasset ×1
core-image ×1
drawrect ×1
instruments ×1
memory-leaks ×1
nsoperation ×1
nspredicate ×1
objective-c ×1
swift ×1
uibutton ×1
uikit ×1
uitextview ×1