我想使用Swift代码在我的应用中正确定位项目,无论屏幕尺寸是多少.例如,如果我想要一个按钮占屏幕宽度的75%,我可以做一些类似于(screenWidth * .75)按钮宽度的事情.我发现这可以通过Objective-C来确定
CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;
Run Code Online (Sandbox Code Playgroud)
不幸的是,我不确定如何将其转换为Swift.有没有人有想法?
谢谢!
我想从文件系统将一些图像加载到我的应用程序中.有两种简单的方法可以做到这一点:
[UIImage imageNamed:fullFileName]
Run Code Online (Sandbox Code Playgroud)
要么:
NSString *fileLocation = [[NSBundle mainBundle] pathForResource:fileName ofType:extension];
NSData *imageData = [NSData dataWithContentsOfFile:fileLocation];
[UIImage imageWithData:imageData];
Run Code Online (Sandbox Code Playgroud)
我更喜欢第一个,因为它的代码少得多,但是我看到有些人说图像被缓存了,而且这个方法使用了更多的内存?由于我不相信大多数其他论坛上的人,我想我会在这里问这个问题,是否有任何实际的区别,如果是的话哪个更"好"?
我尝试使用对象分配工具分析我的应用程序,我看不出任何实际差异,虽然我只在模拟器中尝试过,而不是在iPhone本身上.
我有一个UITableViewCell,它有两个UITextFields(没有边框).以下约束用于设置水平布局.
@"|-10-[leftTextField(>=80)]-(>=10)-[rightTextField(>=40)]-10-|"
没有什么花哨的,并且按预期工作.

如您所见,上部textField具有正确的大小.较低的textField以空文本开头,由于空文本,它是80点宽.当我在编辑textField中键入文本时,文本向左滚动,它不会改变其宽度.
我不喜欢这样,当用户键入textField时,textField的宽度应该调整.
在我看来,应该开箱即用.通过实施IBAction该UIControlEventEditingChanged事件,我可以确认实际输入改变intrinsicContentSize的也是UITextField的.
但是,在textField不再是第一个响应者之前,宽度不会改变.如果我将光标放入另一个textField,则设置已编辑的textField的宽度.对于我想要的东西,这有点晚了.
这些注释掉的行是我尝试过的,没有任何成功:
- (IBAction)textFieldDidChange:(UITextField *)textField {
[UIView animateWithDuration:0.1 animations:^{
// [self.contentView removeConstraints:horizontalConstraints];
// [self.contentView addConstraints:horizontalConstraints];
// [self.contentView layoutIfNeeded];
// [self.contentView setNeedsLayout];
// [self.contentView setNeedsUpdateConstraints];
}];
NSLog(@"%@", NSStringFromCGSize(textField.intrinsicContentSize));
}
Run Code Online (Sandbox Code Playgroud)
有人知道我错过了什么吗?我有什么办法让这项工作成功?
随着iOS版的64位版本,我们不能使用%d和%u再格式化NSInteger和NSUInteger.因为对于64位,这些是typedef'd long而unsigned long不是int和unsigned int.
因此,如果您尝试使用%d格式化NSInteger,Xcode将发出警告.Xcode对我们很好,并提供了这两种情况的替代品,它包括一个带有l前缀的格式说明符和一个长的类型转换.然后我们的代码基本上是这样的:
NSLog(@"%ld", (long)i);
NSLog(@"%lu", (unsigned long)u);
Run Code Online (Sandbox Code Playgroud)
如果你问我,这是一个痛苦的眼睛.
几天前,Twitter上有人提到格式说明符%zd来格式化签名变量和%tu在32位和64位平台上格式化无符号变量.
NSLog(@"%zd", i);
NSLog(@"%tu", u);
Run Code Online (Sandbox Code Playgroud)
这似乎有效.而且我更喜欢类型转换.
但老实说,我不知道为什么这些工作.现在两者对我来说基本上都是神奇的价值观.
我做了一些研究,并发现z前缀意味着以下格式说明符具有相同的大小size_t.但我完全不知道前缀是什么t意味着.所以我有两个问题:
究竟是什么%zd和%tu意味着什么呢?
是否可以安全使用%zd,%tu而不是苹果建议长期使用?
我知道类似的问题和Apples 64位过渡指南,它们都推荐这种%lu (unsigned long)方法.我要求替代铸造.
在这样一个简单的例子中,我可以省略self来引用backgroundLayer,因为它明确设置了backgroundColor的backgroundLayer.
class SpecialView: UIView {
let backgroundLayer = CAShapeLayer()
init() {
backgroundLayer.backgroundColor = UIColor.greenColor().CGColor
}
}
Run Code Online (Sandbox Code Playgroud)
但是,就像在Objective-C中一样,我们可以通过添加名称相似的局部变量(或常量)来混淆事物.现在,backgroundColor正在非形状图层上设置:
class SpecialView: UIView {
let backgroundLayer = CAShapeLayer()
init() {
var backgroundLayer = CALayer()
backgroundLayer.backgroundColor = UIColor.greenColor().CGColor
}
}
Run Code Online (Sandbox Code Playgroud)
(这是通过使用self.backgroundLayer.backgroundColor解决的)
在Objective-C中,我始终避免使用ivars来获取属性和属性,为了清晰起见,它们总是以self为前缀.我不必担心快速的ivars,但是我应该在swift中使用self时还有其他考虑因素吗?
我有一个包含Autolayout和Size Classes的故事板.相当复杂的布局,不幸的是我无法确定如何在新项目中重现问题.
但是有问题的视图被固定到屏幕的左边缘和右边缘,其约束具有750优先级(即|-(0@750)-[myView]-(0@750)-|,另外它具有大于或等于约束的优先级为1000(即|-(>=0)-[myView]-(>=0)-|).这样做是为了限制在iPad上的宽度,所以在容器约束中存在宽度约束width <= 600 @1000和中心水平.最重要的是,视图的宽高比约束为3:1.正如我所说,相当复杂.
Interface Builder不会显示约束的任何问题.Xcode布局预览为所有设备呈现正确.
当我运行应用程序iOS时告诉我它有相互冲突的约束.
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't …Run Code Online (Sandbox Code Playgroud) 看这个高低,但找不到我的答案.我期待查询所有不等于指定字符串的记录的核心数据.例如,所有记录不等于当前会话ID.我试过这些无济于事:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"listingID != %@", [sitListingID objectAtIndex:i]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@ NOT CONTAINS[cd] %@",@"listingID", [sitListingID objectAtIndex:i]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@ NOT CONTAINS %@",@"listingID", [sitListingID objectAtIndex:i]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"listingID NOT like %@", [sitListingID objectAtIndex:i]];
Run Code Online (Sandbox Code Playgroud)
没什么用.HEEEEELP !!! -------------------------------------------------- ---更多代码
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"ListingRecord" inManagedObjectContext:context];
[request setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"sessionID <> %@", uniqueSessionListings];
[request setPredicate:predicate];
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[context executeFetchRequest:request error:&error] mutableCopy];
Run Code Online (Sandbox Code Playgroud) 在一些IBAction我看到:
- (IBAction)pushButton:(id)sender;
Run Code Online (Sandbox Code Playgroud)
该(id)sender什么时候使用它?
我目前遵循几个教程,对XCODE开发相当新.
陷入意想不到的问题 - 寻求帮助!
问题是"UIViewController子类"没有出现在文件模板库中.我的书和我检查的每个页面都有这个模板在iOS或Mac"Cocoa touch"类别下.我的xcode只显示"Objective-C Class""Objective-C Category""Objective-C Class Extension""Objective-C protocol""Objective-C Test Case Class"
如何添加UIViewController子类的新文件模板?
我的环境: - Snow Leopard - xcode版本:4.3
提前致谢!
ios ×5
cocoa-touch ×3
swift ×3
autolayout ×2
iphone ×2
objective-c ×2
core-data ×1
ibaction ×1
nsinteger ×1
nspredicate ×1
printf ×1
sender ×1
uitextfield ×1
xcode ×1
xcode4.3 ×1