我尝试Swift在我的app中集成代码.我的应用程序是写入的Objective-C,我添加了一个Swift类.我做了这里描述的所有事情.但我的问题是Xcode没有创建-Swift.h文件,只有桥接头.所以我创造了它,但它实际上是空的.我可以在Swift中使用我所有的ObjC类,但我不能这样做.我标记了我的快速课程,@objc但没有帮助.我现在能做什么?
编辑:Apple说:"当您将Swift代码导入Objective-C时,您依靠Xcode-generated头文件将这些文件暴露给Objective-C.[...]此标题的名称是您的产品模块名称,然后添加" -Swift.h"."
现在,当我想导入该文件时,它会出错:
//MainMenu.m
#import "myProjectModule-Swift.h" //Error: 'myProjectModule-Swift.h' file not found
@implementation MainMenu
Run Code Online (Sandbox Code Playgroud)
这是我的FBManager.swift文件:
@objc class FBManager: NSObject {
var descr = "FBManager class"
init() {
super.init()
}
func desc(){
println(descr)
}
func getSharedGameState() -> GameState{
return GameState.sharedGameState() //OK! GameState is written in Objective-C and no error here
}
}
Run Code Online (Sandbox Code Playgroud) 我试图使用/etc/hosts文件将一些网站重定向到localhost.为此,我在终端中打开它sudo nano /etc/hosts然后我修改了文件,并保存了它.作为最后一步,我刷新了DNS缓存sudo killall -HUP mDNSResponder.
这是我的hosts文件的样子:
$ cat /etc/hosts
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 somethig.com
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
Run Code Online (Sandbox Code Playgroud)
这根本行不通.我试图重新启动,没有运气.我也做了很多研究,但没有在互联网上找到任何有效的答案.操作系统是否仍然使用此文件,或者这从未真正起作用?
我正在使用macOS 10.13.
假设我有以下结构:
var Partner = require('../partner/partner.model');
var ContractSchema = new Schema({
// blahblah
partners: [Partner.schema]
});
Run Code Online (Sandbox Code Playgroud)
所以这里每个合约都有一组合作伙伴,每个合作伙伴都有一个_id属性。
我想搜索一份合同,该合同的合作伙伴具有特定的_id. 我试过这个,但没有用:
var ObjectId = require('mongoose').Types.ObjectId;
Contract.find({
partners: {
$elemMatch: {
_id: {
$eq: new ObjectId(req.query.partnerId)
}
}
}
}, function (err, serviceContracts) {
if (err) {
return handleError(res, err);
} else {
return res.status(200).json(serviceContracts);
}
});
Run Code Online (Sandbox Code Playgroud)
编辑:这是我在数据库中的示例:
{
"_id" : ObjectId("57e370e72beac9fc21c99ca3"),
"updatedAt" : ISODate("2016-09-22T05:49:27.000Z"),
"createdAt" : ISODate("2016-09-22T05:49:27.000Z"),
"currency" : "USD",
"partners" : [
{
"updatedAt" : ISODate("2016-09-22T05:49:27.000Z"),
"createdAt" …Run Code Online (Sandbox Code Playgroud) 假设,我有一个用C编写的程序,我有两台相同的计算机,一台运行Windows,另一台运行Linux.由于计算机是相同的,它们的处理器具有相同的指令集,因此编译后的机器代码应该相同.那么为什么我需要两次编译我的程序呢?假设,我不会调用任何与操作系统相关的功能,或者取决于实际操作系统的功能.
我有一个nodejs/expressjs后端服务,我希望使用端点注册我的设备.我必须使用一些json编码数据向我的服务器发送POST请求.我这样做很麻烦.我可以成功发送GET请求,并从服务器获得响应,但是当我尝试发送POST请求时,我得不到任何响应.我是这样做的:
//Make a post request
void postRequest(const char* url, const char* host, String data){
if(client.connect(host, PORT)){
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
//"Connection: close\r\n" +
"Content-Type: application/json\r\n" +
"Content-Length: " + data.length() + "\r\n" +
data + "\n");
//Delay
delay(10);
// Read all the lines of the reply from server and print them to Serial
CONSOLE.println("Response: \n");
while(client.available()){
String line = client.readStringUntil('\r');
CONSOLE.print(line);
}
}else{
CONSOLE.println("Connection to backend failed.");
return;
} …Run Code Online (Sandbox Code Playgroud) 我为类型矩阵创建了一个矩阵模板类T.我收到一个链接器错误和一个警告,上面写着:Instantiation of variable 'ndmatrix<unsigned int>::Subscriptable::_data' required here, but no definition is available.
模板在单独的标头中定义:
ndmatrix.h:
Run Code Online (Sandbox Code Playgroud)
#ifndef ndmatrix_h
#define ndmatrix_h
#include <vector>
#include <functional>
#include <algorithm>
//Generic matrix class to hold the table levels of type T
template<class T>
class ndmatrix{
public:
size_t R; //rows
size_t C; //cols
//Hack to use [][] operator on std::vector
class Subscriptable{
public:
//Default constructor
Subscriptable(){};
Subscriptable(int idx, size_t R, size_t C) : _idx(idx), _R(R), _C(C) {
for (int i = 0; i<_C*_R; …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 JSContext 中运行的 javacript 中使用本机 Swift 代码。
例如,我在 Swift 中实现了这个类:
class Greeter: NSObject {
public func greet() -> String {
return "Hello World!"
}
public func greetMe(_ name: String) -> String {
return "Hello, " + name + "!"
}
}
Run Code Online (Sandbox Code Playgroud)
然后我使用 JSContext 来运行 javascript 代码:
let context = JSContext()!
context.setObject(Greeter.self, forKeyedSubscript: "Greeter" as (NSCopying & NSObjectProtocol))
// Try my native functions:
let jsv1 = context.evaluateScript("Greeter.greet()")!
let jsv2 = context.evaluateScript("Greeter.greetMe(\"Jon Arbuckle\")")!
print("Greeter.greet() = \(jsv1)") // prints Greeter.greet() = undefined
print("Greeter.greatMe(\"Jon …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用express.js返回一个文件作为对GET请求的响应我写了一个简单的FileManager类来处理文件请求,但是'undefined is not a function'当我调用时,我收到了错误new FileManager()
这是我如何尝试这样做:
//FileManager.js
FileManager = function () {}; //Empty initializer
FileManager.prototype.handleFileRequest = function(req,res){
var fileId = req.params.id
if(fileId){
var path = './uploads/events' + fileId;
res.sendfile(path)
} else {
res.send(404, 'file not found')
}
}
Run Code Online (Sandbox Code Playgroud)
这是服务器:
//server.js
var express = require('express');
var FileManager = require('./FileManager.js').FileManager;
var app = express();
var fileman = new FileManager();
app.get('/:id', function (req, res){
console.log('get id:' + req.params.id);
fileman.handleFileRequest(req,res);
})
app.listen('blabla',3000);
Run Code Online (Sandbox Code Playgroud)
但是当我跑步时node server.js,我收到以下错误:
var fileman …Run Code Online (Sandbox Code Playgroud)