簡體   English   中英

在Objective-C中使用C函數(適用於iPhone)

[英]Using a C function in Objective-C (for iPhone)

'好吧。 我是一個自我描述的iPhone編程招執的人(擁有更長的perl和網絡背景 - 30年)......但上周冒了一大筆錢買了幾本好書。 在填寫並閱讀了超過1000頁之后 - 並且非常了解它,我正在前往一個良好的第一個原生iPhone應用程序。 我的問題是:我不知道如何在Objective-C中做一個簡單的Geographic(lat / long)多邊形點例程。 我有2種方法可以做到這一點。 一個在C(第一個代碼示例)和一個在JavaScript(第二個代碼示例):

// this is the poly.h file

int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy);


// this is the poly.c file

#include "poly.h"
int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy){
 int i, j, c = 0;
 for (i = 0, j = nvert-1; i < nvert; j = i++) {
 if ( ((verty[i]>testy) != (verty[j]>testy)) &&
  (testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
    c = !c;
 }
 return c;
}

或者這個(在Javascript中):

function _isPointInPoly(poly, pt){
 for(var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i)
  ((poly[i].y <= pt.y && pt.y < poly[j].y) || (poly[j].y <= pt.y && pt.y < poly[i].y))
  && (pt.x < (poly[j].x - poly[i].x) * (pt.y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x)
  && (c = !c);
 return c;
}

(如果我可以讓他們轉換,任何一個都會工作)

所以,試試這個...我把.h文件和.c文件放到我的iPhone項目的xcode中。 現在唯一的問題是如何從Objective-C調用它並獲得結果.. :)

順便說一句:昨晚我搜索了大神Google以獲得答案,但只是嘗試搜索“在Objective-C iPhone應用程序中包含C”等等。你得到了這么多條目而且沒有任何關系! :)只是讓你知道我在發布之前試過谷歌。

好的,我的問題:

  1. 如何從Objective-C調用pnpoly?
  2. 我稱之為什么類型? (int很好,但float * vertx顯然是一個浮點數組...... NSArray沒有 - 我能找到)

(編輯:這里是更多的信息。我要求幫助解決那些將被傳遞的陣列)

這個問題沒有得到充分的質疑。

例程(在objective-c中)將是這樣的:(假設這是正確的編碼)

NSMutableArray *latitudeArray = [[NSMutableArray alloc] init];
NSMutableArray *longitudeArray = [[NSMutableArray alloc] init];

// coordinates surrounding 1 inifite loop.

[latitudeArray addObject:@"37.32812557141369"];
[longitudeArray addObject:@"-122.0320253896318"];
[latitudeArray addObject:@"37.32821852349916"];
[longitudeArray addObject:@"-122.0289014325174"];
[latitudeArray addObject:@"37.33021046381746"];
[longitudeArray addObject:@"-122.0289300638158"];
[latitudeArray addObject:@"37.33042111092124"];
[longitudeArray addObject:@"-122.0279574092159"];
[latitudeArray addObject:@"37.33395972491337"];
[longitudeArray addObject:@"-122.0279263955651"];
[latitudeArray addObject:@"37.33363270879559"];
[longitudeArray addObject:@"-122.0320527775551"];
[latitudeArray addObject:@"37.32812557141369"];
[longitudeArray addObject:@"-122.0320253896318"];


int nvert = [[latitudeArray count] intvalue];

// 37.33189399206268 x -122.0296274412866 should return true

float testx =37.33189399206268;
float testy =-122.0296274412866;

int y_or_n = pnpoly(int nvert, float *vertx, float *verty, float testx, float testy);

我應該明確表示我正在學習Objective-c但是找到了C例程 - 所以不知道如何構造C變量來調用它或者用例程來調用它。

我知道這是在問很多......但這對我來說真是令人費解。 誰能幫我? 非常感謝。

Jann

你可以打電話給它。 Objective-C只是C API的前端和一種將方法重寫為函數的方法(無論如何......),因此您可以像在C代碼中一樣調用C函數。

- (int)doWhatever {
  // ...
  int hitTest = pnPoly(/*blah*/);
  return hitTest;
}

您可以在Objective-C中使用C基本類型(如int和float)而不會出現問題。 所以用浮點數調用函數:)。 如果需要在NSArray等Foundation集合類中存儲這些值,則可以將它們包裝在名為NSNumber的類中。

NSNumber *someFloat = [NSNumber numberWithFloat: f];
float usefulValue = [someFloat floatValue];

以下是如何使用它的示例:

int nCoords = 4;
float vertexXCoords[n] = {0.0, 0.0, 20.0, 20.0};
float vertexYCoords[n] = {0.0, 20.0, 20.0, 0.0};
NSPoint testPoint = NSMakePoint(5, 10);

BOOL testPointIsInPoly = pnpoly(nCoords, xCoords, yCoords, testPoint.x, testPoint.y);

請注意,這里沒有特定於Objective-C的內容。 這是C代碼(盡管它確實使用了Cocoa BOOL和NSPoint C類型)。 由於Objective-C是C的嚴格超集,因此任何有效的C代碼也是有效的Objective-C。 這也是Objective-C的獨特功能不會特別有用的情況。 (一般來說,數值計算在簡單的C中不那么復雜和可讀性。)

objective C是C的超集,所以你可以調用C例程。 如果您經常調用該例程,則將其內聯或使其成為宏。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM