“Converting” a function pointer to a block in objective-C

后端 未结 3 2153
星月不相逢
星月不相逢 2021-02-20 07:48

I\'m doing some Interop from Mono C# to Obj-C and ran into this problem. The C# code needs to pass a callback - which it does with a function pointer. I can get the function poi

3条回答
  •  鱼传尺愫
    2021-02-20 08:30

    A block is under the hood a pointer to a local data structure. A block becomes invalid as soon as you leave the scope where it was declared. The scope is the if-statement within init; as soon as you leave that, the block is invalid.

    You are breaking coding conventions here in a bad way. First, instance variables should start with an underscore, so that everyone sees what you are doing. Better to use properties without declaring instance variables at all. And every block property should be declared as "copy". If you do that, everything is fine.

提交回复
热议问题