在Objective-C中,最佳做法是:
在.h中声明诸如按钮之类的对象,然后在.m中进行合成
.h
@interface SomeViewController : UIViewController
@property (strong, nonatomic) UIButton *someButton;
@end
.m
@implementation SomeViewController
@synthesize someButton = _someButton;
@end
Run Code Online (Sandbox Code Playgroud)或者在.m中将它们声明为ivars
@interface SomeViewController ()
@property (strong, nonatomic) UIButton *someButton;
@end
Run Code Online (Sandbox Code Playgroud)我注意到在许多Apple代码中,特别是他们的Breadcrumbs示例代码,他们的许多属性都在界面中声明.这两者有区别吗?我还注意到,当声明属性时@interface
,它们会自动用下划线前缀someButton = _someButton
合成,使得合成无用.
背景
我正在Swift中构建一个API框架.我希望它包含一个Reachability
可以在内部和外部使用的类.
状态
我复制了Reachability
Tony Million提供的类的源代码,将import语句添加到我的标题中.该.h
文件在Headers
部分中设置为public Build Phases
.我已经设置Allow Non-modular Includes in Framework Modules
到YES
在我的两个项目和目标.每当我去构建项目时,我都会收到错误:
Include of non-modular header inside framework module 'MyFramework.Reachability'
Run Code Online (Sandbox Code Playgroud)
在进口线上ifaddrs.h
.
问题
如何构建项目?是否有另一个我需要链接的框架,不是SystemConfiguration
吗?
我正在尝试构建一个应用程序来构建和保存类似于map run的路由.我用面包屑示例代码,特别是CrumbPath
和CrumbPathView
我的路线的基础上,从苹果.两个问题:
如果我尝试访问类似的MKMapPoint *points
对象,CrumbPath
那么:
[_route lockForReading];
NSLog(@"%@", _route.points);
NSLog(@"%d", _route.pointCount);
[_route unlockForReading];
Run Code Online (Sandbox Code Playgroud)
我的应用程序崩溃,说:
Thread 1: EXC_BAD_ACCESS (code: 1, address: 0x9450342d)
Run Code Online (Sandbox Code Playgroud)
我很难理解,因为在CrumbPath.m
文件中,苹果的人通过显式获取写锁定然后解锁它来写入"数组",但是如果我获取了读锁并试图从中读取它,那么崩溃.
我尝试访问的原因points
是尝试获取MKMapPoints
,将它们转换为CLLocationCoordinate2D
对象,并保存它们,以便我可以polyline
根据用户的请求重绘.由于我无法访问points
,我尝试保存我发送到数组中的CLLocationCoordinate2D
对象以上传到我的Parse后端,但我总是收到错误说:locationManager
_route
Sending 'CLLocationCoordinate2D' to parameter of incompatible type 'id'
Run Code Online (Sandbox Code Playgroud)
这并没有使这更容易.有没有人知道为什么我会收到这些错误?
位置经理代表
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
if (_userLocation.longitude != manager.location.coordinate.longitude
&& _userLocation.latitude != manager.location.coordinate.latitude) {
_userLocation = manager.location.coordinate;
}
if (_isRecording) …
Run Code Online (Sandbox Code Playgroud) 我在切换secureEntry
房产方面遇到了问题UITextField
.切换属性时,将调整字符大小,但光标位于错误的位置:
我有一个视频源,它使用基于AVPlayer的自定义视频播放器来播放HLS流.在导航栏上,有一个用于启动摄像头的按钮.从iOS 7.0.3更新开始,捕获会话现在遇到SERIOUS问题,它会启动,然后立即自行拆除,发出如下错误:
Capture session error:
NSConcreteNotification 0x146eae70 {name = AVCaptureSessionRuntimeErrorNotification; object = <AVCaptureSession: 0x15976e70 [AVCaptureSessionPresetHigh]>
<AVCaptureDeviceInput: 0x145d3ee0 [Back Camera]> -> <AVCaptureVideoDataOutput: 0x159546f0>
<AVCaptureDeviceInput: 0x1594bd60 [iPhone Microphone]> -> <AVCaptureAudioDataOutput: 0x159823e0>
<AVCaptureDeviceInput: 0x145d3ee0 [Back Camera]> -> <AVCaptureVideoPreviewLayer: 0x1467b370>; userInfo = {
AVCaptureSessionErrorKey = "Error Domain=AVFoundationErrorDomain Code=-11819 \"Cannot Complete Action\" UserInfo=0x146f0ec0 {NSLocalizedRecoverySuggestion=Try again later., NSLocalizedDescription=Cannot Complete Action}";
}}
Run Code Online (Sandbox Code Playgroud)
我设置捕获会话的方法看起来像这样......
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(captureSessionDidStartRunning:) name:AVCaptureSessionDidStartRunningNotification object:captureSession];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(captureSessionDidStopRunning:) name:AVCaptureSessionDidStopRunningNotification object:captureSession];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(captureSessionDidFailWithError:) name:AVCaptureSessionRuntimeErrorNotification object:captureSession];
discontinuous = NO;
_recording = NO; …
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一些内容来列出连接到设备的所有用户的Twitter帐户UIActionSheet
.例如,我的设备上有三个Twitter帐户.我希望操作表能够使用取消按钮列出我的帐户.目前,我的功能如下:
- (void)showAlertViewForAccounts:(NSArray*)accounts {
accounts = _.arrayMap(accounts, ^id(ACAccount *account) {
return account.username;
});
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Choose an account:"
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:nil];
for (NSString *username in accounts) {
[actionSheet addButtonWithTitle:username];
}
[actionSheet showInView:[[[[[UIApplication sharedApplication] delegate] window] rootViewController] view]];
}
Run Code Online (Sandbox Code Playgroud)
我的问题是取消按钮不会显示在操作表的单独"部分"中.
无论如何,我可以A.)将accounts
数组转换为a va_list
作为UIActionSheet
init...
方法的参数传入,或B.)指定取消按钮应显示在单独的"部分"?
背景:
我设计了TableViewDataSource
一个为UITableViewDataSource
和提供实现的类UITableViewDelegate
.您实例化TableViewSection
对象,这些对象将传递给TableViewDataSource
用于配置单元格,节标题,句柄选择,行插入等的对象.
该TableViewSection
对象具有一个名为的属性dataSource: [AnyObject]?
,该属性在设置时用于计算该部分中的行数,并为该单元配置块提供一个对象:
// get the section, dequeue a cell for that section, retrieve the item from the dataSource
// ...
tableSection.cellConfigurationBlock?(cell: AnyObject, item: AnyObject?, indexPath: NSIndexPath)
return cell
Run Code Online (Sandbox Code Playgroud)
我想做的是从我viewModel
到我的数组中分配对数组的引用tableSection.dataSource
,让我viewModel
更新数组,然后更新表视图.在Swift中,您不能通过引用传递数组.解决方法似乎是使用a NSMutableArray
,但随之而来的是类型安全性的损失,以及从Swift到Foundation来回转换对象时的更大认知负担.
工作实例:
let kCellIdentifier = "SomeCellIdentifier"
class MyViewController: UITableViewController {
// Property declarations
@IBOutlet var tableDataSource: TableViewDataSource!
var viewModel: MyViewControllerViewModel = MyViewControllerViewModel()
override func viewDidLoad() {
super.viewDidLoad()
self.setupTableView() …
Run Code Online (Sandbox Code Playgroud) 如果tableview包含少于10个左右的单元格,你会如何强制UITableViewCell滚动到顶部?我支持在tableview处于编辑模式时编辑tableView单元格中的托管对象上下文.不用说,如果一个单元格位于tableview的底部,当用户去编辑事件的标题或位置时,它会被键盘阻挡.我试过像这样的解决方案:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
if(_selectedIndex == indexPath.row){
_selectedIndex = -1;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
return;
}
if(_selectedIndex >= 0){
NSIndexPath *previous = [NSIndexPath indexPathForRow:_selectedIndex inSection:0];
_selectedIndex = indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:previous]
withRowAnimation:UITableViewRowAnimationFade];
}
_selectedIndex = indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[tableView setContentOffset:CGPointMake(0, [tableView rowHeight]*indexPath.row) animated:YES];
}
Run Code Online (Sandbox Code Playgroud)
但这并没有将tableview保留在contentOffset上.它将捕捉到顶部,然后快速回弹
NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp4"];
NSLog(@"Path: %@", path);
NSURL *url = [NSURL URLWithString:path];
NSLog(@"URL: %@", url);
NSData *data = [NSData dataWithContentsOfURL:url];
NSLog(@"Data exists? %@", data ? @"yes" : @"no");
Run Code Online (Sandbox Code Playgroud)
test.mp4在我的包中,但这始终是输出
2013-03-13 11:50:29.444 videoAppPrototype[28625:907] Path: /var/mobile/Applications/D66DD820-4DE1-4E45-9495-9BACF130E368/videoAppPrototype.app/test.mp4
2013-03-13 11:50:29.446 videoAppPrototype[28625:907] URL: /var/mobile/Applications/D66DD820-4DE1-4E45-9495-9BACF130E368/videoAppPrototype.app/test.mp4
2013-03-13 11:50:29.448 videoAppPrototype[28625:907] Data exists? no
Run Code Online (Sandbox Code Playgroud)
我不知道为什么,但数据从未存在过.
_topBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 45)];
UIImageView *bar = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"top_bar.png"]];
bar.frame = CGRectMake(0, 0, 320, 45);
[_topBar addSubview:bar];
_up = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"up.png"]];
[_topBar addSubview:_up];
Run Code Online (Sandbox Code Playgroud)
_up没有显示在我的构建中.如果我为我的资源文件夹中的任何其他图像交换"up.png",它会显示,但up.png拒绝.有什么我能做的吗?我只使用UIImage,静态分配的UIImageView初始化它,删除并重新添加.png文件,将它们从资源文件夹中取出并放在其他地方,重新保存它们,除了抛出它们之外的所有内容.
编辑: 该文件保存为up.png但真的是up.png.psd.只是阅读细则的问题.
ios ×7
objective-c ×2
swift ×2
acaccount ×1
avfoundation ×1
class-design ×1
cllocation ×1
coding-style ×1
foundation ×1
ios7 ×1
ios8 ×1
iphone ×1
mkmapview ×1
mp4 ×1
nsdata ×1
nsurl ×1
reachability ×1
twitter ×1
uiimage ×1
uiimageview ×1
uitableview ×1
uitextfield ×1
xcode ×1