2014-03-27 66 views
1

C文件我有这个代码在一个我的视图控制器:如何包括iOS的项目

int randomNumber = (arc4random() % 1) + 6; 

因为我需要它在更多的地方,我决定做它的功能。 但是我把它当做C函数,老习惯很难死。

现在我有文件WOC_Random.c与此内容

#include <stdio.h> 
#include <stdlib.h> // for arc4random() function 

#ifndef WOC_Random_C 
#define WOC_Random_C 


int randomInt(int startInt, int endInt) 
{ 
    int randomNumber = (arc4random() % startInt) + endInt; 

    return randomNumber; 
} 

#endif 

现在,在我的视图控制器代码:

int randomNumber = randomInt(1, 6); 

但我在连接有问题,这是错误:

duplicate symbol _randomInt in: 
/Users/Mac/Library/Developer/Xcode/DerivedData/GuessTheNumber-gjovdrsarctubnbqhczqukvahwgb/Build/Intermediates/GuessTheNumber.build/Debug-iphonesimulator/GuessTheNumber.build/Objects-normal/i386/GTN_FirstViewController.o 
/Users/Mac/Library/Developer/Xcode/DerivedData/GuessTheNumber-gjovdrsarctubnbqhczqukvahwgb/Build/Intermediates/GuessTheNumber.build/Debug-iphonesimulator/GuessTheNumber.build/Objects-normal/i386/WOC_Random.o 
ld: 1 duplicate symbol for architecture i386 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

我对问题有模糊的理解。
但不知道如何解决?
那么如何解决它,我需要一些参数链接器或编译器?

此外,如果是这样,当我只是有一些简单的功能实现是什么做的iOS发展的最佳途径,因为C函数或者是它更好地做到这一点作为对象C类的功能?

+0

你在WOC_Random.h中有什么? –

+0

就像你有一个.m和一个用于Objective-C代码的.h文件一样,你应该有一个.c和一个.h文件,用于纯C代码(因为Objective-C只是c的扩展,你可以使它.m和.h文件;这样,如果你喜欢,你的C代码也可以调用Objective-C函数)。如果它们属于类,则应该创建类方法。您不应该创建一个类,以便您可以将C函数作为类方法使用。 – gnasher729

回答

3

您需要添加头文件(如WOC_Random.h),其中,你会声明函数

int randomInt(int startInt, int endInt); 

然后定义在WOC_Random.c该功能。然后在要使用该函数的类中包含WOC_Random.h。