Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check object is kind of block or not

How can we identify any particular object is kind of block or not?

for example,

NSSet *set =[NSSet setWithObjects:
             @"name1",
             @"name2",
             [^{ /* ..... some code */ } copy],
             nil];

How can we find out which object from set is kind of block?

like image 712
Abhijeet Avatar asked Jul 12 '11 07:07

Abhijeet


People also ask

How can you tell if an object is type?

Use the typeof operator to get the type of an object or variable in JavaScript. The typeof operator also returns the object type created with the "new" keyword. As you can see in the above example, the typeof operator returns different types for a literal string and a string object.

How do you check if an object belongs to a certain class in C#?

To determine whether an object is a specific type, you can use your language's type comparison keyword or construct. For example, you can use the TypeOf… Is construct in Visual Basic or the is keyword in C#. The GetType method is inherited by all types that derive from Object.

How do you check object is created or not in Java?

In the main method, we created an object of the User2 class using the new keyword and called the getUser1Object() method. It returns an object of class User1 , which we later store in getUser1Object . To check if it is null, we call the isNull() method and pass the object getUserObject as a parameter.


5 Answers

There is a safer way to determine if something is a block without actually using private api or constructing a class using the private string name:

- (BOOL)isBlock:(id)item {
    id block = ^{};
    Class blockClass = [block class];
    while ([blockClass superclass] != [NSObject class]) {
        blockClass = [blockClass superclass];
    }
    return [item isKindOfClass:blockClass];
}
like image 65
Micah Hainline Avatar answered Oct 14 '22 12:10

Micah Hainline


Wrap your block in a class of your own:

BlockWrapper *blockWrapper = [BlockWrapper wrapperWithBlock:^{ … }];

Check for the type and extract the actual block:

if ([obj isKindOfClass:[BlockWrapper class]]) {
    codeBlock = [(BlockWrapper*)obj block];
}
like image 20
Pierre Bernard Avatar answered Oct 14 '22 10:10

Pierre Bernard


There is no supported way to do this. You must keep track of what objects are blocks, and what their type signatures are.

Do you have a practical use case for a set of mixed strings and blocks?

like image 36
Jens Ayton Avatar answered Oct 14 '22 12:10

Jens Ayton


It's possible, but I wouldn't recommend doing this, because NSBlock is not a public class and its name might change in the future:

if ([obj isKindOfClass:NSClassFromString(@"NSBlock")]) {
    NSLog(@"It's a block!");
}
like image 41
omz Avatar answered Oct 14 '22 11:10

omz


If you only have strings and blocks, just check ![thing isKindOfClass:[NSString class]]. i.e. invert your test.

Likewise, if you have strings, numbers and blocks, check that thing is not a string or a number, and in that case it must (by deduction) be a block. Either that, or your program is incorrect and will crash.

like image 37
al45tair Avatar answered Oct 14 '22 10:10

al45tair