【问题标题】:Use of undeclared identified variable使用未声明的已识别变量
【发布时间】:2017-03-07 10:08:22
【问题描述】:

我第一次尝试从官方 appcelerator 文档:iOS Module Quick Start 构建用于 Titanium 应用程序的原生 iOS 模块。不知道为什么构建失败

'/iphone/Classes/ComExampleTestModule.m:55:20: Use of undeclared identifier 'foo'; did you mean 'for'? 

我注意到 deallocsetExampleProp 的这个错误,我在 ComExampleTestModule.m 文件中使用 foo 变量。

ComExampleTestModule.h

#import "TiModule.h"

@interface ComExampleTestModule : TiModule
{
}

@end

@interface ComAppcNewmoduleModule : TiModule
{
    NSString *foo;
}
@end

ComExampleTestModule.m

/**


* test
 *
 * Created by Your Name
 * Copyright (c) 2017 Your Company. All rights reserved.
 */

#import "ComExampleTestModule.h"
#import "TiBase.h"
#import "TiHost.h"
#import "TiUtils.h"

@implementation ComExampleTestModule

#pragma mark Internal

// this is generated for your module, please do not change it
-(id)moduleGUID
{
    return @"e5d1b415-6588-4911-9825-3a210032b430";
}

// this is generated for your module, please do not change it
-(NSString*)moduleId
{
    return @"com.example.test";
}

#pragma mark Lifecycle

-(void)startup
{
    // this method is called when the module is first loaded
    // you *must* call the superclass
    [super startup];

    NSLog(@"[INFO] %@ loaded",self);
}

-(void)shutdown:(id)sender
{
    // this method is called when the module is being unloaded
    // typically this is during shutdown. make sure you don't do too
    // much processing here or the app will be quit forceably

    // you *must* call the superclass
    [super shutdown:sender];
}

#pragma mark Cleanup

-(void)dealloc
{
    // release any resources that have been retained by the module
    RELEASE_TO_NIL(foo); // Errors out on this line
    [super dealloc];
}

#pragma mark Internal Memory Management

-(void)didReceiveMemoryWarning:(NSNotification*)notification
{
    // optionally release any resources that can be dynamically
    // reloaded once memory is available - such as caches
    [super didReceiveMemoryWarning:notification];
}

#pragma mark Listener Notifications

-(void)_listenerAdded:(NSString *)type count:(int)count
{
    if (count == 1 && [type isEqualToString:@"my_event"])
    {
        // the first (of potentially many) listener is being added
        // for event named 'my_event'
    }
}

-(void)_listenerRemoved:(NSString *)type count:(int)count
{
    if (count == 0 && [type isEqualToString:@"my_event"])
    {
        // the last listener called for event named 'my_event' has
        // been removed, we can optionally clean up any resources
        // since no body is listening at this point for that event
    }
}

#pragma Public APIs

-(id)example:(id)args
{

    return @"hello world";
}

-(id)exampleProp:(id)foo
{
    NSLog(@"[INFO] In Module - the stored value for exampleProp: %@", foo);
    return foo;
}

-(void)setExampleProp:(id)value
{
    // Macro from TiBase.h to type check the data
    ENSURE_STRING(value);
    // Call the retain method to keep a reference to the passed value
    foo = [value retain]; // Errors out on this line
    NSLog(@"[INFO] In Module - the new value for exampleProp: %@", value);
}

@end

【问题讨论】:

  • 你在 .m 文件上尝试过@synthesize foo 吗?
  • 不,@RicardoAlves 只是按照文档中的说明进行了尝试。并做了一个小改动,显式地将 foo 传递给 setExampleProp,这不起作用。
  • fooComAppcNewmoduleModule 的成员,而不是 ComExampleTestModule。但是您尝试在ComExampleTestModule(@implementation ComExampleTestModule) 中访问它。这就是您收到错误消息的原因。
  • 感谢@Larme,这是一个蹩脚的错误。你介意回答这个问题,这样我就可以关闭这个投票给你了吗?

标签: ios objective-c xcode titanium-mobile appcelerator-titanium


【解决方案1】:

fooComAppcNewmoduleModule 的成员,而不是ComExampleTestModule。但是您尝试在ComExampleTestModule (@implementation ComExampleTestModule) 中访问它。
这就是您收到错误的原因。因为ComExampleTestModule 不知道foo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-21
    • 1970-01-01
    相关资源
    最近更新 更多