标签: nsinvocation

NSInvocation和block之间有什么区别?

当我说阻止我的意思是:

^(int a) {return a*a;};
Run Code Online (Sandbox Code Playgroud)

此外,块仅支持iOS4及更高版本.

这两者有什么区别?

objective-c nsinvocation objective-c-blocks

5
推荐指数
1
解决办法
1050
查看次数

调用阻止iOS

我尝试调用一些块,但我遇到了一个EXC_BAD_ACCESS.

-(void) methodA {
   self.block = ^ {
       [self methodB];
   };
}

-(void) webViewDidFinishLoad:(UIWebView *)webView {
       [block invoke]; // error here (block is not valid id type).
}

-(void)methodB {
    //do something
}
Run Code Online (Sandbox Code Playgroud)

有关为什么会发生这种情况的任何想法?

objective-c nsinvocation ios objective-c-blocks

5
推荐指数
2
解决办法
2万
查看次数

NSInvocationOperation回调太快了

我知道有几次问过类似的问题,但是我很难理解这个特殊问题是如何解决的.到目前为止,我所做的一切都是在主要方面进行的.我现在发现我需要执行一个需要一些时间的操作,并且我希望在操作期间向我的显示器添加HUD并在操作完成时将其淡出.

在阅读了很多关于GCD(并且变得非常困惑)之后,我决定最简单的方法是使用NSInvocationOperation调用我耗时的方法并将其添加到新创建的NSOperationQueue中.这就是我所拥有的:

        [self showLoadingConfirmation]; // puts HUD on screen

        // this bit takes a while to draw a large number of dots on a MKMapView            
        NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
                                                                                selector:@selector(timeConsumingOperation:)
                                                                                  object:[self lotsOfDataFromManagedObject]];

        // this fades the HUD away and removes it from the superview
        [operation setCompletionBlock:^{ [self performSelectorOnMainThread:@selector(fadeConfirmation:) withObject:loadingView waitUntilDone:YES]; }];

        NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
        [operationQueue addOperation:operation];
Run Code Online (Sandbox Code Playgroud)

我希望这能显示HUD,开始在地图上绘制点,然后一旦完成该操作,就会消除HUD.

相反,它显示HUD,开始在地图上绘制点,并在仍然绘制点的同时淡化HUD.根据我的NSLogs,在调用淡入HUD的方法之前,有大约四分之一秒的延迟.与此同时,点的绘制持续了几秒钟.

我可以做些什么让它等到地图上的绘图完成后才会消失HUD?

谢谢

编辑添加:

在进行以下更改后,我几乎成功了:

        NSInvocationOperation *showHud = [[NSInvocationOperation alloc] initWithTarget:self
                                                                              selector:@selector(showLoadingConfirmation)
                                                                                object:nil];

        NSInvocationOperation *operation = …
Run Code Online (Sandbox Code Playgroud)

nsoperation nsoperationqueue nsinvocation ios nsinvocationoperation

5
推荐指数
1
解决办法
448
查看次数

在objective-c中,当对象本身被指定为它实现的委托时,对象如何返回不同的代理对象

我有一个实现各种协议的对象(比如10个不同的协议).

例如

@interface MyClass <UITableViewDelegate,UITableViewDataSource,UISearchDisplayDelegate,...>

@end

@implementation

/// a whole bunch of methods for the delegates
@end
Run Code Online (Sandbox Code Playgroud)

为了在这个类中"清理"东西 - 我创建了辅助类,它封装了与这些委托相关的逻辑.

所以现在新的重构类看起来像

// public interface which looks the same
@interface MyClass <UITableViewDelegate,UITableViewDataSource,UISearchDisplayDelegate,...>


@end

// private interface

@interface MyClass ()

// a bunch of objects which implement those methods
@property (nonatomic,strong) MyUITableviewDelegate *tableViewDelegate;
@property (nonatomic,strong) MyUITableviewDataSource *tableViewDelegate;
@property (nonatomic,strong) MySearchDisplayDelegate *searchDisplayDelegate;
// another bunch of objects which answer to the delegates
@end

@implementation
// all the delegate methods were moved out of …
Run Code Online (Sandbox Code Playgroud)

delegates objective-c objective-c-runtime nsinvocation ios

5
推荐指数
1
解决办法
989
查看次数

使用OCMock的块期望值

我正在使用GHUnit和OCMock在我的iOS应用程序中进行一些测试工作.

所以我在整合它时遇到了一些麻烦.

以下代码运行良好.

NSString *s = [NSString stringWithString:@"122"];
id mock = [OCMockObject partialMockForObject:s];
[[[mock stub] andReturn:@"255"] capitalizedString];
NSString *returnValue = [mock capitalizedString];
GHAssertEqualObjects(returnValue, @"255", @"Should be equal");
[mock verify];
Run Code Online (Sandbox Code Playgroud)

但是当我改变[[[mock stub] andReturn:@"255"] capitalizedString]; 成

[[[mock stub] andDo:^(NSInvocation *invocation) {
    [invocation setReturnValue:@"255"];
}] capitalizedString];
Run Code Online (Sandbox Code Playgroud)

我收到一条错误,上面写着"原因:'NSCFString'应该等于'255'.应该相等"

我认为这两个陈述应该完全相同.我错了吗?

unit-testing objective-c ocmock nsinvocation objective-c-blocks

4
推荐指数
1
解决办法
2944
查看次数

NSInvocation&NSError - __autoreleasing&memory crasher

在了解NSInvocations时,似乎我对内存管理的理解存在差距.

这是一个示例项目:

@interface DoNothing : NSObject
@property (nonatomic, strong) NSInvocation *invocation;
@end

@implementation DoNothing
@synthesize invocation = _invocation;

NSString *path = @"/Volumes/Macintosh HD/Users/developer/Desktop/string.txt";

- (id)init
{
    self = [super init];
    if (self) {

        SEL selector = @selector(stringWithContentsOfFile:encoding:error:);
        NSInvocation *i = [NSInvocation invocationWithMethodSignature:[NSString methodSignatureForSelector:selector]];

        Class target = [NSString class];
        [i setTarget:target];
        [i setSelector:@selector(stringWithContentsOfFile:encoding:error:)];

        [i setArgument:&path atIndex:2];

        NSStringEncoding enc = NSASCIIStringEncoding;
        [i setArgument:&enc atIndex:3];

        __autoreleasing NSError *error;
        __autoreleasing NSError **errorPointer = &error;
        [i setArgument:&errorPointer atIndex:4];

        // I understand that I need to …
Run Code Online (Sandbox Code Playgroud)

objective-c nsinvocation automatic-ref-counting

4
推荐指数
1
解决办法
2015
查看次数

在我的应用程序中找到了一个N​​SZombie ......现在怎么样?

我有一个NSManagedObject的子类,有一些"整数32"属性,真的是枚举.这些枚举在我的模型的.h文件中定义,如下所示:

typedef enum {
    AMOwningCompanyACME,
    AMOwningCompanyABC,
    AMOwningCompanyOther
} AMOwningCompany;
Run Code Online (Sandbox Code Playgroud)

我需要显示一个表视图,显示此自定义对象的每个属性的值,因此对于每个枚举,我有一个看起来像这样的方法来返回字符串值:

-(NSArray*)stringsForAMOwningCompany
{
    return [NSArray arrayWithObjects:@"ACME Co.", @"ABC Co.", @"Other", nil];
}
Run Code Online (Sandbox Code Playgroud)

在我的表视图中,我遍历my的属性NSManagedObject(使用NSEntityDescription's' attributesByName和每个属性我调用一个调用相应"stringsFor"方法的辅助方法来返回该特定属性的字符串:

-(NSArray*)getStringsArrayForAttribute:(NSString*)attributeName
{
    SEL methodSelector = NSSelectorFromString([self methodNameForAttributeNamed:attributeName]);
    NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:[[AMProperty class] instanceMethodSignatureForSelector:methodSelector]];
    [invocation setSelector:methodSelector];
    [invocation setTarget:self.editingPole];
    [invocation invoke];

    NSArray* returnValue = nil;
    [invocation getReturnValue:&returnValue];

    return returnValue;
}
Run Code Online (Sandbox Code Playgroud)

我的表格视图cellForRowAtIndexPath如下所示:

...
NSString* itemName = self.tableData[indexPath.row];
NSAttributeDescription* desc = itemAttributes[itemName];

NSString* cellIdentifier = [self cellIdentifierForAttribute:desc]; // checks the attribute type and …
Run Code Online (Sandbox Code Playgroud)

debugging malloc objective-c nsinvocation ios

4
推荐指数
1
解决办法
506
查看次数

为什么NSInvocation getReturnValue:丢失对象?

我需要你的帮助.我在NSInvocation'getReturnValue:'方法中遇到了一些问题.我想以编程方式创建UIButton,甚至更多,我想使用NSInvocation动态创建它,并通过NSArray传递值(这就是我包装UIButtonTypeRoundedRect的原因).

清单.

NSLog(@"Button 4 pushed\n");//this code executed when button pushed
Class cls = NSClassFromString(@"UIButton");//if exists {define class},else cls=nil
SEL msel = @selector(buttonWithType:);
//id pushButton5 = [cls performSelector:msel withObject:UIButtonTypeRoundedRect];//this code works correctly,but I want to do this by NSInvocation
//---------------------------
NSMethodSignature *msignatureTMP;
NSInvocation *anInvocationTMP;

msignatureTMP = [cls methodSignatureForSelector:msel];
anInvocationTMP = [NSInvocation invocationWithMethodSignature:msignatureTMP];
[anInvocationTMP setTarget:cls];
[anInvocationTMP setSelector:msel];

UIButtonType uibt_ = UIButtonTypeRoundedRect;
NSNumber *uibt = [NSNumber numberWithUnsignedInt:uibt_];
NSArray *paramsTMP;
paramsTMP= [NSArray arrayWithObjects:uibt,nil];

id currentValTMP = [paramsTMP objectAtIndex:0];//getParam from NSArray
NSInteger i=2;
void* bufferTMP; …
Run Code Online (Sandbox Code Playgroud)

pointers objective-c nsinvocation

3
推荐指数
1
解决办法
4385
查看次数

为什么这个NSInvocation会引发异常?

我现在真的很头疼.因此,一个NSTimer对象,一个NSMethodSignature对象和一个NSInvocation对象走进一个吧.这是其余的笑话:

NSMethodSignature *methodSig = [NSMethodSignature methodSignatureForSelector:@selector(setAlphaValue:)];
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:methodSig];
CGFloat alphaVal = 1.f;

[inv setSelector:@selector(setAlphaValue:)];
[inv setTarget:tabViewItem.view];
[inv setArgument:&alphaVal atIndex:2];

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.5f invocation:inv repeats:NO];
Run Code Online (Sandbox Code Playgroud)

这是我在调试控制台中获得的内容:

+[NSInvocation _invocationWithMethodSignature:frame:]: method signature argument cannot be nil

编辑:我不确定为什么有人认为有必要对我的问题进行投票.很抱歉试图学习新东西.实际上,这是对我原来问题的修正:我应该采取哪些不同的做法?一旦我弄清楚我的问题是什么,我应该刚刚删除了帖子吗?我试图遵循所有的Stack Overflow礼仪,我甚至花时间给出一个可以帮助别人的机会.下一次,我应该回到我的问题并留下一个回应,比如"nvm ......想通了.你好吗?" 或者我应该把它留在这里,没有答案?我肯定知道我已经厌倦了点击链接只能导致无人接听的帖子.

exception objective-c method-signature nstimer nsinvocation

3
推荐指数
1
解决办法
1424
查看次数

使用NSInvocation时,arm64上的EXC_BAD_ACCESS崩溃

我已经开始准备一个旧项目来支持arm64架构.但是当我尝试在64位设备上执行此代码时,我在[invocation retainArguments]上遇到EXC_BAD_ACCESS崩溃; 线

- (void)makeObjectsPerformSelector: (SEL)selector withArguments: (void*)arg1, ...
{

    va_list argList;

    NSArray* currObjects = [NSArray arrayWithArray: self];
    for (id object in currObjects)
    {
        if ([object respondsToSelector: selector])
        {
            NSMethodSignature* signature = [[object class] instanceMethodSignatureForSelector: selector];

            NSInvocation* invocation = [NSInvocation invocationWithMethodSignature: signature];
            invocation.selector = selector;
            invocation.target = object;

            if (arg1 != nil)
            {
                va_start(argList, arg1);

                char* arg = arg1;

                for (int i = 2; i < signature.numberOfArguments; i++)
                {
                    const char* type = [signature getArgumentTypeAtIndex: i];
                    NSUInteger size, align;
                    NSGetSizeAndAlignment(type, …
Run Code Online (Sandbox Code Playgroud)

nsinvocation ios arm64

3
推荐指数
1
解决办法
1394
查看次数