2014-12-04 24 views
0

我試圖創建一個擴展,它可以讓我展現一個警告消息,在iOS的兩個按鈕與把手事件:OpenFL擴展原生的iOS(C++,haXe的,OBJ-C)

我有一個問題每次我試圖編譯OBJ-C部分:

我在這裏失去了(注意我不是很自信OBJ-C):

登錄:

NativeTest.mm 
    \f0\b0 \cf0 -oobj/iphoneos-v7/04964c13_NativeTest.o\ 

\f1\b \cf3 Error: 
\f0\b0 \cf0 
\f1\b ./iphone/NativeTest.mm:20:32: error: missing '[' at start of message send expression 
\f0\b0 \ 

\f1\b   UIAlertView* alertView[[UIAlertView alloc] initWithTitle: [[NSString alloc] 
\f0\b0 \ 

\f1\b        ^
\f0\b0 \ 

\f1\b        [ 
\f0\b0 \ 

\f1\b ./iphone/NativeTest.mm:25:107: warning: missing sentinel in method dispatch [-Wsentinel] 
\f0\b0 \ 

\f1\b           otherButtonTitles: [[NSString alloc] initWithUTF8String:cancel]] 
\f0\b0 \ 

\f1\b                           ^
\f0\b0 \ 

\f1\b                           , nil 
\f0\b0 \ 

\f1\b /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 
\f0\b0 \ 

\f1\b - (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."); 
\f0\b0 \ 

\f1\b^
\f0\b0 \ 

\f1\b ./iphone/NativeTest.mm:27:9: error: expected ']' 
\f0\b0 \ 

\f1\b   [alert show]; 
\f0\b0 \ 

\f1\b  ^
\f0\b0 \ 

\f1\b ./iphone/NativeTest.mm:20:31: note: to match this '[' 
\f0\b0 \ 

\f1\b   UIAlertView* alertView[[UIAlertView alloc] initWithTitle: [[NSString alloc] 
\f0\b0 \ 

\f1\b        ^
\f0\b0 \ 

\f1\b ./iphone/NativeTest.mm:20:32: error: size of array has non-integer type 'UIAlertView *' 
\f0\b0 \ 

\f1\b   UIAlertView* alertView[[UIAlertView alloc] initWithTitle: [[NSString alloc] 
\f0\b0 \ 

\f1\b        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
\f0\b0 \ 

\f1\b 1 warning and 3 errors generated. 
\f0\b0 \ 

\f1\b \cf2 xcrun 
\f0\b0 \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 
\f1\b -DIPHONE=IPHONE 
\f0\b0 
\f1\b -DIPHONESIM=IPHONESIM 
\f0\b0 -Wno-parentheses -Wno-null-dereference -Wno-unused-value -Wno-bool-conversion 
\f1\b -DSTATIC_LINK 
\f0\b0 
\f1\b -DHXCPP_VISIT_ALLOCS 
\f0\b0 
\f1\b -DHXCPP_API_LEVEL=0 
\f0\b0 -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/ 
\f1\b NativeTest.mm 
\f0\b0 \cf0 -oobj/iphonesim/aa8846ca_NativeTest.o\ 

\f1\b \cf3 Error: 
\f0\b0 \cf0 
\f1\b ./iphone/NativeTest.mm:20:32: error: missing '[' at start of message send expression 
\f0\b0 \ 

\f1\b   UIAlertView* alertView[[UIAlertView alloc] initWithTitle: [[NSString alloc] 
\f0\b0 \ 

\f1\b        ^
\f0\b0 \ 

\f1\b        [ 
\f0\b0 \ 

\f1\b ./iphone/NativeTest.mm:25:107: warning: missing sentinel in method dispatch [-Wsentinel] 
\f0\b0 \ 

\f1\b           otherButtonTitles: [[NSString alloc] initWithUTF8String:cancel]] 
\f0\b0 \ 

\f1\b                           ^
\f0\b0 \ 

\f1\b                           , nil 
\f0\b0 \ 

\f1\b /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 
\f0\b0 \ 

\f1\b - (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."); 
\f0\b0 \ 

\f1\b^
\f0\b0 \ 

\f1\b ./iphone/NativeTest.mm:27:9: error: expected ']' 
\f0\b0 \ 

\f1\b   [alert show]; 
\f0\b0 \ 

\f1\b  ^
\f0\b0 \ 

\f1\b ./iphone/NativeTest.mm:20:31: note: to match this '[' 
\f0\b0 \ 

\f1\b   UIAlertView* alertView[[UIAlertView alloc] initWithTitle: [[NSString alloc] 
\f0\b0 \ 

\f1\b        ^
\f0\b0 \ 

\f1\b ./iphone/NativeTest.mm:20:32: error: size of array has non-integer type 'UIAlertView *' 
\f0\b0 \ 

\f1\b   UIAlertView* alertView[[UIAlertView alloc] initWithTitle: [[NSString alloc] 
\f0\b0 \ 

\f1\b        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
\f0\b0 \ 

\f1\b 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 

我NativeTest.h部分

#ifndef NativeTest 
#define NativeTest 


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

#endif 

ExternalInterface.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 
      } 
    } 
} 

回答

0

錯誤消息表明編譯器想要一個左括號。

你是否像你的應用程序一樣複製你的代碼?在Objective-C方面,它看起來像缺少了一些東西,即賦值運算符「=」,「nil」用於指示otherButtonTitles的選項結束,並且「 」;「在方法調用結束時。

對於編譯器,從「UIAlertView *」到「[show alertview]」;看起來像一個單一的聲明。

順便說一句你也可以使用[NSString stringWithUTF8String:]來代替alloc/init'所有的字符串。同樣的效果,少代碼:

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