开发者

How to Convert code to Obj-C?

How do I convert this to Objective-C?? Mainly this: float *newH= new float[newD];

What do I substitute new for in obj-c??

int newD = 开发者_如何转开发100;

float *newH = new float[newD];

for(int i=0; i<newD; i++){
    newH[i] = 0.0f;
}


Objective-C is a true superset of C. In C, one uses malloc to allocate memory.

So,

float *newH = malloc( newD * sizeof( float) );

Of course, depending on what you are really doing, you may also want to investigate NSArray and NSNumber.

Note, that Objective-C++ is available as well and you can continue to use 'new' to allocate your memory.


If you have a lot of C++ code to port, you might be better off writing your app in Objective-C++. Then you can use your C++ code as is.


I agree that Objective C++ is the way to go. However, if you absolutely want to stay within the spirit of Objective C (not straight C), here are the options:

  • use an NSArray of NSNumber objects. This is slow - float boxing penalty applies.
  • use a NSData as a allocation mechanism, cast [bytes] to float *. This is ugly, the way pointer typecasts are.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜