在Obj C中初始化全局变量

时间:2015-11-06 20:38:22

标签: objective-c

我正在尝试初始化全局变量,但是我不确定我做错了什么,因为我收到错误。

现在,我有一个Globals.h文件,我将其导入AppDelegate。在这里,我宣布:

-(void) initializeCompetencies{ <arrays with values>} 

然后,我不确定在哪里定义这些数组。我在didFinishWithLaunchingOptions方法的AppDelegate.m和ViewController的viewDidLoad方法中尝试过,我创建了一个单独的方法,

Undefined symbols for architecture x86_64:
  "_Competencies", referenced from:
  -[QuestionViewController viewDidLoad] in QuestionViewController.o
  -[QuestionViewController changed:] in QuestionViewController.o
  -[QuestionViewController nextMilestone:] in QuestionViewController.o
  -[QuestionViewController pressBack:] in QuestionViewController.o
  -[QuestionViewController initializeCompetencies] in QuestionViewController.o
  "_CompetencyEight", referenced from:
      -[QuestionViewController initializeCompetencies] in QuestionViewController.o
  "_CompetencyEleven", referenced from:
      -[QuestionViewController initializeCompetencies] in QuestionViewController.o
  "_CompetencyFive", referenced from:
      -[QuestionViewController initializeCompetencies] in QuestionViewController.o
  "_CompetencyFour", referenced from:
      -[QuestionViewController initializeCompetencies] in QuestionViewController.o
  "_CompetencyNine", referenced from:
      -[QuestionViewController initializeCompetencies] in QuestionViewController.o
  "_CompetencyOne", referenced from:
      -[QuestionViewController initializeCompetencies] in QuestionViewController.o
  "_CompetencySeven", referenced from:
      -[QuestionViewController initializeCompetencies] in QuestionViewController.o
  "_CompetencySix", referenced from:
      -[QuestionViewController initializeCompetencies] in QuestionViewController.o
  "_CompetencyTen", referenced from:
      -[QuestionViewController initializeCompetencies] in QuestionViewController.o
  "_CompetencyThree", referenced from:
      -[QuestionViewController initializeCompetencies] in QuestionViewController.o
  "_CompetencyTwelve", referenced from:
      -[QuestionViewController initializeCompetencies] in QuestionViewController.o
  "_CompetencyTwo", referenced from:
      -[QuestionViewController initializeCompetencies] in QuestionViewController.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
在ViewController中

,但我一直得到同样的错误:

    apply plugin: 'com.android.application'

android {
compileSdkVersion 21
buildToolsVersion "22.0.1"

defaultConfig {
    applicationId "done.ui"
    minSdkVersion 16
    targetSdkVersion 21
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}
repositories {
    mavenCentral()
        maven {
    url "https://jitpack.io"
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:recyclerview-v7:22.2.+'
compile 'com.google.android.gms:play-services:7.5.0'
compile 'com.facebook.android:facebook-android-sdk:4.2.0'
compile 'com.pubnub:pubnub-android:3.7.4'
compile 'com.amazonaws:aws-android-sdk-core:2.+'
compile 'com.amazonaws:aws-android-sdk-cognito:2.+'
compile 'com.amazonaws:aws-android-sdk-ddb:2.+'
compile 'com.amazonaws:aws-android-sdk-ddb-mapper:2.+'
compile 'com.android.support:cardview-v7:21.0.+'
compile 'com.github.clans:fab:1.5.5'
compile 'com.melnykov:floatingactionbutton:1.3.0'
compile 'com.github.ksoichiro:android-observablescrollview:1.5.0'
compile 'com.nineoldandroids:library:2.4.0'
compile 'com.github.kanytu:android-parallax-recyclerview:v1.4'
compile files('libs/PayPalAndroidSDK-2.8.5.jar')
}

如何正确声明和初始化全局数组?或者,如何解决此错误?谢谢!

4 个答案:

答案 0 :(得分:2)

除了NSString常量之外,Objective-C对象只在运行时创建,因此您不能使用表达式来初始化它们,否则您将得到错误:初始化元素是不是编译时常量

如果您声明:

//Globals.h
extern NSArray *CompetencyOne;

//Globals.m
NSArray *CompetencyOne = nil; //this works coz nil is compile time constant

请查看link

答案 1 :(得分:0)

声明和初始化是不够的。您还必须定义变量。

添加Globals.m文件并复制声明,删除extern

// Globals.m:

NSArray *CompetencyOne;
NSArray *CompetencyTwo;
NSArray *CompetencyThree;
...

答案 2 :(得分:0)

Sooo,数组很棘手,因为它们不会编译时间常数。你必须这样做:

Private Sub UserForm_Initialize() 'Declare variables Const CMMData As String = "\\ATSTORE01\CMMData\21064D\21064D-OP400.dat" Dim strSN As Collection Dim strSet As Collection Dim strFF As Collection Dim strVHCC As Collection Dim LineData As String Dim SplitData() As String Dim LineIter As Long Set strSN = New Collection Set strSet = New Collection Set strFF = New Collection Set strVHCC = New Collection 'Populate Set Number Listbox With New Scripting.FileSystemObject With .OpenTextFile(CMMData, ForReading) Do Until .AtEndOfStream LineData = .ReadLine SplitData = Split(LineData, ",", 154, vbTextCompare) 'Extracting Serial Number strSN.add SplitData(0) 'Extracting Set Number strSet.add SplitData(1) 'Extracting Final Flow Area strFF.add SplitData(14) 'Extracting /V/ To Hook CC strVHCC.add SplitData(31) Set_Select.AddItem SplitData(1) LineIter = LineIter + 1 Loop

Constants.h

extern NSArray *myArray;

Constants.m

然后,在应用程序初始化的某个地方,可能是app委托完成了启动调用你的函数:

NSArray *myArray = nil;

答案 3 :(得分:0)

这听起来更像是你想要一个对象创建不可变数组以供应用程序内消费。

一个名为CompentencyFactory的类有多种静态方法怎么样:

+ (NSArray *) competencyOne;
+ (NSArray *) competencyTwo;

等,看起来像这样:

+ (NSArray *) competencyOne {
    return @[
        @"foo",
        @"bar",
        @"fubnut"
    ];
}

您可以在整个应用中引用,如:

NSArray *comp = [CompetencyFactory competencyOne];