我有一组动画需要按顺序操作,每个步骤都要进行各种检查.因为集合的大小是在运行时确定的,所以我希望使用递归调用...但是我无法让它在"块"范例内运行.
无论我是否使用预先声明的块,结果都是EXEC_BAD_ACCESS
__block void (^myBlock)(BOOL) = ^(BOOL finished){ if (finished) [self nextStep];};
Run Code Online (Sandbox Code Playgroud)
或不,如下面的代码片段所示.在调试时,似乎'self'变量确实有效.
NSEnumerator* stepEnumerator;
-(void) mainRoutine {
stepEnumerator = [myArray objectEnumerator];
[self nextStep];
}
-(void) nextStep {
id obj;
if ((obj = [stepEnumerator nextObject])) {
// Do my checking at each location
....
// we have another spot to move to
[UIView animateWithDuration:animationDuration
animations:^{self.frame = newFrame;}
completion:^(BOOL finished){ if (finished) [self nextStep];}];
}
}
else {
// we are done, finish house cleaning
}
}
Run Code Online (Sandbox Code Playgroud)
任何帮助是极大的赞赏.
谢谢!
我有一个按钮,在我的应用程序(UIButton的子类)中发光.要为发光设置动画,请使用以下方法
- (void)glow
{
[UIView animateWithDuration:1.0f
delay:0.0f
options:UIViewAnimationOptionCurveEaseInOut
animations:^(void){
if (glowingImageView_.alpha != 1.0f) {
[glowingImageView_ setAlpha:1.0f];
}
else {
[glowingImageView_ setAlpha:0.3f];
}
}
completion:^(BOOL finnished){
if (on_) {
[self glow];
}
}];
}
Run Code Online (Sandbox Code Playgroud)
它在iOS 5上运行良好,但在iOS 4.3中我的应用程序停止处理任何用户交互.任何人都知道可能导致这种情况的原因是什么?
谢谢
我有一个可以向上滑动的ViewController.单击"保存"时,它会向服务器发送请求.请求完成后,它会关闭ViewController.我将NSURLConnection切换到使用async和blocks(https://github.com/rickerbh/NSURLConnection-Blocks).解除ViewController现在抛出"线程2:程序接收信号:EXC_BAD_ACCESS".如果重要的话,我正在使用ARC.
- (IBAction) savePressed
{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://api.com/items/create"]];
//NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[NSURLConnection asyncRequest:request success:^(NSData *data, NSURLResponse *response) {
[self close];
} failure:^(NSData *data, NSError *error) {
[self close];
}];
}
- (void) close
{
[self dismissViewControllerAnimated:YES completion:nil];
}
Run Code Online (Sandbox Code Playgroud)
这是日志
2012-10-24 10:32:43.780 Prayrbox[22268:1703] bool _WebTryThreadLock(bool), 0x1f21fd90: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit …Run Code Online (Sandbox Code Playgroud) 我正在使用NSOperationQueue排队并调用多个地理编码位置查找.我想在所有异步运行的查找完成后调用完成方法.
-(void)geocodeAllItems {
NSOperationQueue *geoCodeQueue = [[NSOperationQueue alloc]init];
[geoCodeQueue setName:@"Geocode Queue"];
for (EventItem *item in [[EventItemStore sharedStore] allItems]) {
if (item.eventLocationCLLocation){
NSLog(@"-Location Saved already. Skipping-");
continue;
}
[geoCodeQueue addOperationWithBlock:^{
NSLog(@"-Geocode Item-");
CLGeocoder* geocoder = [[CLGeocoder alloc] init];
[self geocodeItem:item withGeocoder:geocoder];
}];
}
[geoCodeQueue addOperationWithBlock:^{
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
NSLog(@"-End Of Queue Reached!-");
}];
}];
}
- (void)geocodeItem:(EventItem *)item withGeocoder:(CLGeocoder *)thisGeocoder{
NSLog(@"-Called Geocode Item-");
[thisGeocoder geocodeAddressString:item.eventLocationGeoQuery completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
NSLog(@"Error: geocoding failed for item %@: %@", item, error); …Run Code Online (Sandbox Code Playgroud) 我正在尝试搜索提醒列表,以检查是否存在提醒.方法中有一个选项可以保存提醒是否存在的值.我正在使用一个块,并且在块内,当找到提醒时,新值被赋予reminderExists变量,就像它应该的那样.但是,当我输入save if语句时,其值reminderExists已从块中指定的值更改.我对这个问题很难过,我已经尝试了很多改变来诊断问题,但没有用.谢谢你的帮助!
- (BOOL)checkForReminderWithTitle:(NSString *)reminderTitle saveChanges:(BOOL)save {
NSNumber *currentGardenCurrentReminders = currentGarden.reminders;
__block NSNumber *reminderExists = [NSNumber numberWithBool:NO];
if (eventStore == nil)
{
eventStore = [[EKEventStore alloc] init];
[eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error) {
if (!granted)
NSLog(@"Access to store not granted");
}];
}
if (eventStore != nil) {
NSPredicate *predicate = [eventStore predicateForIncompleteRemindersWithDueDateStarting:[NSDate distantPast] ending:[NSDate distantFuture] calendars:[NSArray arrayWithObject:[eventStore defaultCalendarForNewReminders]]];
[eventStore fetchRemindersMatchingPredicate:predicate completion:^(NSArray *reminders) {
for (EKReminder *reminder in reminders) {
NSLog(@"%@", reminderTitle);
if ([reminder.title isEqualToString:reminderTitle]) { …Run Code Online (Sandbox Code Playgroud) 我正在使用Swift,并想知道是否有一种方法可以为现有的objective-c块分配一个闭包.
fromObjC?.performBlock = {someVar in /*do something*/}
Run Code Online (Sandbox Code Playgroud)
它给了我一个错误"无法分配给这个表达式的结果".
我有一个方法,其中包含一个在 Objective-C 中定义的块:
+(void)getNewList:(NewListRequestModel *)model returnInfo:(void(^)(NewListResponseModel* resModel))retModel;
Run Code Online (Sandbox Code Playgroud)
我像这样调用它:
[API getNewList:model returnInfo:^(NewListResponseModel *resModel) {
//code
}];
Run Code Online (Sandbox Code Playgroud)
在 Objective-C 中。
现在我想在 Swift 3.2 中调用它:
API.getNewList(model, returnInfo: {(resModel: NewListResponseModel) -> () in
//my code
})
Run Code Online (Sandbox Code Playgroud)
但我总是遇到错误:
Cannot convert value of type '(NewListResponseModel) -> Void' to expected argument type '((NewListResponseModel?) -> Void)!'
Run Code Online (Sandbox Code Playgroud)
有人可以帮助我以正确的方式调用它吗?谢谢。
嗨iam在objective-c中使用Blocks.我刚学习语法,以及如何编写块.但我不明白执行流程.我用Google搜索了执行流程,我找不到.
我使用了以下代码:
@interface NSArray (Extended)
- (NSArray *)each:(void (^)(id))block;
@end
@implementation NSArray (Extended)
- (NSArray *)each:(void (^)(id object))block {
for(id mObject in self)
block(mObject);
return self;
}
@end
int main (int argc, const char * argv[]) {
@autoreleasepool {
NSArray *array = [NSArray arrayWithObjects:@"Number one", @"Number two", nil];
[array each:^(id object) {
NSLog(@"obj: %@", object);
}];
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释objective-c中块的执行流程是什么?
我遇到了这个场景,当我在一个nil对象上调用一个块时,我的应用程序崩溃了exec_bad_access.我能够通过添加if条件来解决问题,但我想知道为什么在nil对象上调用块会导致bad_access?
@interface CustomView :UIView
@property (nonatomic, strong) UIImage* sourceImage;
@property (nonatomic, copy) void(^doneSwipingBlock)();
- (void)testMethod;
@end
//Another Class
//Sample Code (this is not the actual code but shows the crash
CustomView view = nil;
view.sourceImage = [UIImage imageNamed:@"image.png"]; //no error as view is nil
[view testMethod]; //no error as view is nil
view.doneSwipingBlock(); //Crashes here
/*
//This works fine
if (view.doneSwipingBlock) {
view.doneSwipingBlock();
}
*/
Run Code Online (Sandbox Code Playgroud) 我正在尝试启动一个块中NSObject调用的子类.本应该是,直到我在设置块.我需要在这个块中设置它,因为在我显示之后我在方法中保存了它FormObjectJavascriptCoreFormObjectnilJavascriptCoreUIActionSheetFormObjectUIActionSheetDelegate- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
这是代码:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (actionSheet.tag == kSaveFormConfirmationActionSheetTag) {
if ([ButtonTitle isEqualToString:NSLocalizedString(@"Yes", @"Yes")]) {
NSLog(@"Saving form data");
NSLog(@"%@",formData);
NSLog(@"%@",formData.dictionary);
if (formData) {
[[AutoFillManager defaultManager] saveFormData:formData];
formData = nil;
}
}
else if ([ButtonTitle isEqualToString:NSLocalizedString(@"Never for this Website", @"Never for this Website")]) {
NSString *formURLString = [formData.urlString copy];
formData = nil;
formData = [[FormObject alloc] initWithDisabledSite:formURLString];
[[AutoFillManager defaultManager] saveFormData:formData];
formData = nil;
}
else …Run Code Online (Sandbox Code Playgroud) 假设我有一个基本的整数迭代,如下所示:
NSInteger rowCount = self.rowCount;
for (int i = 0; i < rowCount; i++) {
// stuff!
}
Run Code Online (Sandbox Code Playgroud)
有没有办法使用快速枚举块来实现它?我当然可以创建一个整数数组0 - self.RowCount,但这似乎并不像这样做那么高效.
ios ×6
objective-c ×6
iphone ×3
swift ×2
animation ×1
closures ×1
cocoa-touch ×1
enumeration ×1
ios4 ×1
ios5 ×1
ivar ×1
swift3 ×1
uiview ×1