OpenFL extension native iOS (c++, haxe, obj-c)



我正在尝试创建一个扩展,允许我在iOS中显示带有两个带有句柄事件的按钮的警报消息:

每次尝试编译时,我都会遇到obj-c部分的问题:

我在这里迷路了(注意我对obj-c不是很有信心):

日志:

NativeTest.mm
    f0b0 cf0  -oobj/iphoneos-v7/04964c13_NativeTest.o
f1b cf3 Error:
f0b0 cf0  
f1b ./iphone/NativeTest.mm:20:32: error: missing '[' at start of message send expression
f0b0 
f1b         UIAlertView* alertView[[UIAlertView alloc] initWithTitle: [[NSString alloc]
f0b0 
f1b                                ^
f0b0 
f1b                                [
f0b0 
f1b ./iphone/NativeTest.mm:25:107: warning: missing sentinel in method dispatch [-Wsentinel]
f0b0 
f1b                                            otherButtonTitles: [[NSString alloc] initWithUTF8String:cancel]]
f0b0 
f1b                                                                                                           ^
f0b0 
f1b                                                                                                           , nil
f0b0 
f1b /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.1.sdk/System/Library/Frameworks/UIKit.framework/Headers/UIAlertView.h:26:1: note: method has been explicitly marked sentinel here
f0b0 
f1b - (instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id /*<UIAlertViewDelegate>*/)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION NS_EXTENSION_UNAVAILABLE_IOS("Use UIAlertController instead.");
f0b0 
f1b ^
f0b0 
f1b ./iphone/NativeTest.mm:27:9: error: expected ']'
f0b0 
f1b         [alert show];
f0b0 
f1b         ^
f0b0 
f1b ./iphone/NativeTest.mm:20:31: note: to match this '['
f0b0 
f1b         UIAlertView* alertView[[UIAlertView alloc] initWithTitle: [[NSString alloc]
f0b0 
f1b                               ^
f0b0 
f1b ./iphone/NativeTest.mm:20:32: error: size of array has non-integer type 'UIAlertView *'
f0b0 
f1b         UIAlertView* alertView[[UIAlertView alloc] initWithTitle: [[NSString alloc]
f0b0 
f1b                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
f0b0 
f1b 1 warning and 3 errors generated.
f0b0 
f1b cf2 xcrun
f0b0 cf0  --sdk iphonesimulator8.1 clang++ -IiPhone/include -Iinclude -c -stdlib=libstdc++ -O2 -fmessage-length=0 -pipe -Wno-trigraphs -fno-stack-protector -fpascal-strings -fasm-blocks -Wreturn-type -Wno-unused-variable -arch i386 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.1.sdk -fvisibility=hidden -miphoneos-version-min=5.0 
f1b -DIPHONE=IPHONE
f0b0  
f1b -DIPHONESIM=IPHONESIM
f0b0  -Wno-parentheses -Wno-null-dereference -Wno-unused-value -Wno-bool-conversion 
f1b -DSTATIC_LINK
f0b0  
f1b -DHXCPP_VISIT_ALLOCS
f0b0  
f1b -DHXCPP_API_LEVEL=0
f0b0  -I/Users/henriknielsen/Desktop/Stencyl-3.0/plaf/haxe/lib/hxcpp/3,1,39/include -x objective-c++ -fobjc-abi-version=2 -fobjc-legacy-dispatch cf2 ./iphone/
f1b NativeTest.mm
f0b0 cf0  -oobj/iphonesim/aa8846ca_NativeTest.o
f1b cf3 Error:
f0b0 cf0  
f1b ./iphone/NativeTest.mm:20:32: error: missing '[' at start of message send expression
f0b0 
f1b         UIAlertView* alertView[[UIAlertView alloc] initWithTitle: [[NSString alloc]
f0b0 
f1b                                ^
f0b0 
f1b                                [
f0b0 
f1b ./iphone/NativeTest.mm:25:107: warning: missing sentinel in method dispatch [-Wsentinel]
f0b0 
f1b                                            otherButtonTitles: [[NSString alloc] initWithUTF8String:cancel]]
f0b0 
f1b                                                                                                           ^
f0b0 
f1b                                                                                                           , nil
f0b0 
f1b /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.1.sdk/System/Library/Frameworks/UIKit.framework/Headers/UIAlertView.h:26:1: note: method has been explicitly marked sentinel here
f0b0 
f1b - (instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id /*<UIAlertViewDelegate>*/)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION NS_EXTENSION_UNAVAILABLE_IOS("Use UIAlertController instead.");
f0b0 
f1b ^
f0b0 
f1b ./iphone/NativeTest.mm:27:9: error: expected ']'
f0b0 
f1b         [alert show];
f0b0 
f1b         ^
f0b0 
f1b ./iphone/NativeTest.mm:20:31: note: to match this '['
f0b0 
f1b         UIAlertView* alertView[[UIAlertView alloc] initWithTitle: [[NSString alloc]
f0b0 
f1b                               ^
f0b0 
f1b ./iphone/NativeTest.mm:20:32: error: size of array has non-integer type 'UIAlertView *'
f0b0 
f1b         UIAlertView* alertView[[UIAlertView alloc] initWithTitle: [[NSString alloc]
f0b0 
f1b                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
f0b0 
f1b 1 warning and 3 errors generated.

这是我 NativeTest.mm 的部分

#include <NativeTest.h>
#import <UIKit/UIKit.h>
#import <CoreFoundation/CoreFoundation.h>
using namespace nativetest;
@interface nativetestController : UIAlertView <UIAlertViewDelegate>
{
}
@end
@implementation nativetestController
namespace nativetest
{
    void showSystemAlert(const char *title, const char *message, const char *ok, const char *cancel)
    {   
        UIAlertView* alertView[[UIAlertView alloc] initWithTitle: [[NSString alloc]
                                                               initWithUTF8String:title]
                                                 message: [[NSString alloc] initWithUTF8String:message] 
                                                 delegate: NULL
                                                 cancelButtonTitle: [[NSString alloc] initWithUTF8String:ok]
                                           otherButtonTitles: [[NSString alloc] initWithUTF8String:cancel]]
        [alert show];
        }
                             }

- (void) alertView: (UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        //Code for OK button
          bool pressid = YES;
    }
    if (buttonIndex == 1)
    {
        //Code for download button
        bool pressid = NO; 
    }
}


@end

我的原生测试.h

#ifndef NativeTest
#define NativeTest

namespace nativetest
{   
void showSystemAlert(const char* title, const char* message, const char* ok, const char* cancel);
    bool pressid();
}
#endif

外部接口.cpp

#if defined(HX_WINDOWS) || defined(HX_MACOS) || defined(HX_LINUX)
#define NEKO_COMPATIBLE
#endif
#include <hx/CFFI.h>
#include "NativeTest.h"
#include <stdio.h>
using namespace nativetest;

#ifdef IPHONE
//--------------------------------------------------
// Glues Haxe to native code.
//--------------------------------------------------
void ios_alert(value title, value message, value ok, value cancel)
{
    showSystemAlert(val_string(title), val_string(message), val_string(ok), val_string(cancel));
}
DEFINE_PRIM(ios_alert, 4);
static value getpressid()
{
    if (nativetest::pressid())
        return val_true;
    return val_false;
}
DEFINE_PRIM(getpressid, 0);
#endif

//--------------------------------------------------
// IGNORE STUFF BELOW THIS LINE
//--------------------------------------------------
extern "C" void nativetest_main() 
{   
}
DEFINE_ENTRY_POINT(nativetest_main);
extern "C" int nativetest_register_prims() 
{ 
    return 0; 
}

NativeTest.hx

 package;
#if cpp
import cpp.Lib;
#elseif neko
import neko.Lib;
#else
import nme.Lib;
#end
#if android
import nme.JNI;
#end
class NativeTest
{   
    private static var pressid:Bool = false;  
    private static var title:String;
    private static var message:String;
    private static var ok:String;
    private static var cancel:String;
    public static function showAlert(title:String, message:String, ok:String, cancel:String):Void
    {
        NativeTest.title = title;
        NativeTest.message = message;
        NativeTest.ok = ok;
        NativeTest.cancel = cancel;
        NativeTest.pressid = pressid;
        haxe.Timer.delay(delayAlert, 30);
    }
    private static function delayAlert():Void
    {
        #if(cpp && mobile && !android)
        iosAlert(title, message, ok, cancel);
        #end
        #if android
        if(androidAlert == null)
        {
            androidAlert = nme.JNI.createStaticMethod("NativeTest", "showAlert", "(Ljava/lang/String;Ljava/lang/String;)V", true);
        }
        androidAlert([title, message]);
        #end
    }
    #if android
    private static var androidAlert:Dynamic;
    #end
    #if(cpp && mobile && !android)
    static var iosAlert = nme.Loader.load("ios_alert", 4);
    private static var pressid = nme.Loader.load("getpressid",0);
    #end
    public static function getpressid(pressid:Int):Bool
    {
        if (pressid == 0)
        {
            #if ios
            return pressid();
            #end
        }
        else
        {
            #if ios
            return pressid();
            #end
            }
    }   
}   

错误消息指示编译器需要左括号。

您是否完全按照应用中的代码复制了代码?在 Objective-C 方面,看起来缺少一些东西,即赋值运算符"="、"nil"表示 otherButtonTitle 的选项结束,以及方法调用末尾的";"。

对于编译器来说,从"UIAlertView*"到"[show alertview];"的所有内容看起来都像一个语句。

顺便说一句,你也可以使用[NSString stringWithUTF8String:]而不是分配/初始化所有的字符串。相同的效果,更少的代码:

    UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:[NSString stringWithUTF8String:title]
                                                        message:[NSString stringWithUTF8String:message]
                                                       delegate:NULL
                                              cancelButtonTitle:[NSString stringWithUTF8String:ok]
                                              otherButtonTitles:[NSString stringWithUTF8String:cancel], nil];

最新更新