我想计算sklablenode文本中的字母数量,这可能吗?
所以如果:
mySklabelnode = SKLabelNode()
mySklabelnode.text = "testing"
比我想要的结果是:7
如何做到这一点?
答案 0 :(得分:0)
使用g++ test.cpp -Llua -Lpulse -o test
。
#include <stdio.h>
#include <stdlib.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <pulse/pulseaudio.h>
class LuaObject {
public:
static const char *Metatable;
static const luaL_Reg Methods[];
static void InitModule(lua_State *L);
static int Create(lua_State *L);
//Retrieve object instance from Lua stack at specified position
static LuaObject* get(lua_State *L, int pos) {
return *(LuaObject**)luaL_checkudata(L, pos, LuaObject::Metatable);
}
};
class Pulse: public LuaObject {
//Here I want to avoid having to declare/define Metatable, get() etc again.
public:
static int getEvent(lua_State *L);
pa_context *ctx;
};
const char *Pulse::Metatable = "pulse"; //error: ISO C++ does not permit ‘LuaObject::Metatable’ to be defined as ‘Pulse::Metatable’ [-fpermissive]
const luaL_Reg Pulse::Methods[] = { //error: ISO C++ does not permit ‘LuaObject::Methods’ to be defined as ‘Pulse::Methods’ [-fpermissive]
{"getEvent", Pulse::getEvent},
{NULL, NULL}
};
//Set up the "pulse" module in a Lua state
static void Pulse::InitModule(lua_State *L) { //error: no ‘void Pulse::InitModule(lua_State*)’ member function declared in class ‘Pulse’
if(luaL_newmetatable(L, Pulse::Metatable)) {
luaL_setfuncs(L, Pulse::Methods, 0);
}
lua_pop(L, 1); //remove metatable from Lua stack, leave in Lua registry
static luaL_Reg lib[] = {
{"new", Pulse::Create},
{NULL, NULL}
};
luaL_newlibtable(L, lib);
lua_setglobal(L, "pulse");
}
//Called from Lua (as pulse.new()) to create a Pulse object
static int Pulse::Create(lua_State *L) { //error: no ‘int Pulse::Create(lua_State*)’ member function declared in class ‘Pulse’
Pulse **ud = (Pulse**)lua_newuserdata(L, sizeof(Pulse*));
*ud = new Pulse();
return 1;
}
//Called from Lua (as myPulse:getEvent()) to read events
int Pulse::getEvent(lua_State *L) {
//We only have L as parameter; the Pulse object is on the Lua stack,
//so we need to read it using luaL_checkudata() (which is handled
//by get())
Pulse *self = Pulse::get(L, 1); //error: invalid conversion from ‘LuaObject*’ to ‘Pulse*’ [-fpermissive]
//pretend get_pulse_event() is defined in pulseaudio.h (the actual mechanism
//is much more complex and wouldn't help the example)
int event = get_pulse_event(self->ctx);
lua_pushinteger(L, event);
return 1;
}
int main(int argc, char **argv) {
lua_State *L = luaL_newstate();
luaL_openlibs(L);
Pulse::InitModule(L);
return luaL_dofile(L, "main.lua");
}
答案 1 :(得分:-1)
'a b c'.hasItem(abc)
只是text
,因此您可以将其投放到String
,然后在其上调用NSString
。
length
在没有投射的Swift中
SKLabelNode *mySklabelnode = [[SKLabelNode alloc] init];
mySklabelnode.text = @"testing";
NSLog(@"%d",mySklabelnode.text.length);
或者在Swift中进行投射
let mySklabelnode = SKLabelNode()
mySklabelnode.text = "testing"
println("\(count(mySklabelnode.text))")
注意:使用这些方法检查字符串计数或长度时,如果字符串中可能使用了emojis,则需要额外考虑,因为它们是多部分UTF-16代码单元的unicode字符