我正在尝试使用Python的setuptools
构建此example的变体。在该示例中,只有一个文件main.cpp
。但是,在我的版本中,我要添加另一个类。因此,一共有三个文件:
main.cpp
#include <pybind11/pybind11.h>
#include "myClass.h"
namespace py = pybind11;
PYBIND11_MODULE(python_example, m) {
m.doc() = R"pbdoc(
Pybind11 example plugin
)pbdoc";
py::class_<myClass>(m, "myClass")
.def(py::init<>())
.def("addOne", &myClass::addOne)
.def("getNumber", &myClass::getNumber)
;
}
myClass.h
#include <pybind11/pybind11.h>
class myClass
{
public:
int number;
myClass();
void addOne();
int getNumber();
};
myClass.cpp
#include <pybind11/pybind11.h>
#include "myClass.h"
myClass::myClass() {
number = 1;
}
void myClass::addOne() {
number = number + 1;
}
int myClass::getNumber() {
return number;
}
如果在示例中使用原始setup.py
文件,则该文件不起作用,因为我需要将myClass.cpp
与main.cpp
链接。如何使用setuptools
来做到这一点?基本上,我正在寻找与CMake的setuptools
等效的target_link_libraries
。
我之所以这样问是因为我在CMake方面的经验很少。对我来说,使用setuptools
会更容易。
答案 0 :(得分:1)
我相信您所寻找的是func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
switch indexPath.section {
case 0 :
//Price Cell
let priceCell = tableView.dequeueReusableCell(withIdentifier: "priceCell", for: indexPath) as? priceTableViewCell
//Fatal error: Unexpectedly found nil while unwrapping an Optional value
if let price:String = store![indexPath.row].price{
priceCell?.priceLabel.text! = price
}else{
priceCell?.priceLabel.text! = "-"
}
return priceCell!
case 1 :
//timeCell
let timeCell = tableView.dequeueReusableCell(withIdentifier: "timeCell", for: indexPath) as? timeTableViewCell
if let time:String = store![indexPath.row].open_time{
timeCell?.timeLabel.text! = time
}else{
timeCell?.timeLabel.text! = "-"
}
return timeCell!
case 2 :
//closedayCell
let closedayCell = tableView.dequeueReusableCell(withIdentifier: "closedayCell", for: indexPath) as? closedayTableViewCell
if let closeday:String = store![indexPath.row].closed_day{
closedayCell?.closedayLabel.text! = closeday
}else{
closedayCell?.closedayLabel.text! = "-"
}
return closedayCell!
default :
print("Default Selected")
}
return cell
}
。我强烈建议您查看以下出色示例:https://github.com/pybind/python_example。它应该能够指导您执行所需的操作。
这是我为您的代码提取的内容。请注意,它基本上是从example复制粘贴。
import UIKit
class StoreViewController:
UIViewController,UICollectionViewDataSource,UICollectionViewDelegate,
UICollectionViewDelegateFlowLayout,UITableViewDelegate,
UITableViewDataSource {
var store_id = ""
var store : [Store]?
var photoPath : [Store.photos]?
var tag : [Store.tags]?
var selectedImage : UIImage?
let defaultValues = UserDefaults.standard
@IBOutlet weak var imageCollectionView: UICollectionView!
@IBOutlet weak var mainImage: UIImageView!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var locationLabel: UILabel!
@IBOutlet weak var UIView: UIView!
@IBOutlet weak var tagCollectionView: UICollectionView!
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
//Collectiopn DetaSources
imageCollectionView.dataSource = self
imageCollectionView.delegate = self
tagCollectionView.dataSource = self
tagCollectionView.delegate = self
//UIView Shadow
let shadowPath = UIBezierPath(rect: UIView.bounds)
UIView.layer.masksToBounds = false
UIView.layer.shadowColor = UIColor.black.cgColor
UIView.layer.shadowOffset = .zero
UIView.layer.shadowOpacity = 0.2
UIView.layer.shadowPath = shadowPath.cgPath
//Request API
let url = URL(string: "http://localhost:8000/store/api?store_id=" + store_id)
let request = URLRequest(url: url!)
let session = URLSession.shared
let encoder: JSONEncoder = JSONEncoder()
encoder.dateEncodingStrategy = .iso8601
encoder.outputFormatting = .prettyPrinted
session.dataTask(with: request){(data, response, error)in if error == nil,
let data = data,
let response = response as? HTTPURLResponse{
let decoder: JSONDecoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
do {
let json = try decoder.decode(Store.self, from: data)
if json != nil {
print(json)
}else{
print("nil")
}
self.store = [json]
self.photoPath = json.photos
self.tag = json.tags
if let imageURL = URL(string: "http://127.0.0.1:8000/photos/" + json.photos[0].path){
DispatchQueue.global().async {
let data = try? Data(contentsOf: imageURL)
if let data = data {
let image = UIImage(data: data)
DispatchQueue.main.async {
self.mainImage.image = image
}
}
}
}
DispatchQueue.main.async {
self.nameLabel.text = json.name
self.locationLabel.text = json.location
}
} catch {
print("error:", error.localizedDescription)
}
}
}.resume()
//print(defaultValues.string(forKey: "id"))
// Image Collection view Layout
let itemSize = UIScreen.main.bounds.width/3.62 - 3.62
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)
layout.itemSize = CGSize(width: itemSize, height: itemSize)
layout.minimumInteritemSpacing = 1
layout.minimumLineSpacing = 1
imageCollectionView.collectionViewLayout = layout
// Tag Collection View
let tagLayout = UICollectionViewFlowLayout()
tagLayout.minimumLineSpacing = 1
tagLayout.minimumInteritemSpacing = 1
tagLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)
tagLayout.itemSize = CGSize(width: 80, height: 24)
tagCollectionView.collectionViewLayout = tagLayout
print("storeID is"+store_id)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// UI coner redius
let uiViewPath = UIBezierPath(roundedRect: UIView.bounds,
byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: 8, height: 8))
let uiViewMask = CAShapeLayer()
uiViewMask.path = uiViewPath.cgPath
UIView.layer.mask = uiViewMask
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//Collection
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == self.imageCollectionView{
let imageCell:UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell",for: indexPath)
let imageView = imageCell.contentView.viewWithTag(1) as! UIImageView
let imageURL = URL(string: "http://127.0.0.1:8000/photos/" + photoPath![indexPath.row].path)
if imageURL == nil {
print("nil")
}else{
DispatchQueue.global().async {
let data = try? Data(contentsOf: imageURL!)
if let data = data {
let image = UIImage(data: data)
DispatchQueue.main.async {
imageCell.layer.masksToBounds = true;
imageCell.layer.cornerRadius = 3
imageView.image = image
}
}
}
}
return imageCell
//Tag collection view
}else if collectionView == self.tagCollectionView{
let tagCell:UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Tag",for: indexPath)
let tagLabel = tagCell.contentView.viewWithTag(2) as! UILabel
tagLabel.text! = tag![indexPath.row].name
tagCell.layer.cornerRadius = 12
return tagCell
}else{
return UICollectionViewCell()
}
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
return 3
}
func tagcollectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return tag?.count ?? 0
}
func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return 1
case 1 :
return 1
case 2 :
return 1
default:
return 0
}
}
//Collection view tap
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == self.imageCollectionView{
if photoPath![indexPath.row].path != nil {
if let imageURL = URL(string: "http://127.0.0.1:8000/photos/" + photoPath![indexPath.row].path ){
DispatchQueue.global().async {
let data = try? Data(contentsOf: imageURL)
if let data = data {
let image = UIImage(data: data)
DispatchQueue.main.async {
self.mainImage.image = image
}
}
}
}
}else{
print("error")
return
}
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
switch indexPath.section {
case 0 :
//Price Cell
let priceCell = tableView.dequeueReusableCell(withIdentifier: "priceCell", for: indexPath) as? priceTableViewCell
if let price:String = store![indexPath.row].price{
priceCell?.priceLabel.text! = price
}else{
priceCell?.priceLabel.text! = "-"
}
return priceCell!
case 1 :
//timeCell
let timeCell = tableView.dequeueReusableCell(withIdentifier: "timeCell", for: indexPath) as? timeTableViewCell
if let time:String = store![indexPath.row].open_time{
timeCell?.timeLabel.text! = time
}else{
timeCell?.timeLabel.text! = "-"
}
return timeCell!
case 2 :
//closedayCell
let closedayCell = tableView.dequeueReusableCell(withIdentifier: "closedayCell", for: indexPath) as? closedayTableViewCell
if let closeday:String = store![indexPath.row].closed_day{
closedayCell?.closedayLabel.text! = closeday
}else{
closedayCell?.closedayLabel.text! = "-"
}
return closedayCell!
default :
print("Default Selected")
}
return cell
}
@IBAction func moreImageBtn(_ sender: Any) {
let store_id = self.store_id
self.performSegue(withIdentifier: "toStorePhotoViewController", sender: store_id)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toStorePhotoViewController"{
let storePhotoViewController = segue.destination as! StorePhotoViewController
storePhotoViewController.store_id = sender as! String
}
}
//Bookmark Button
@IBAction func bookmarkBtn(_ sender: Any) {
let user_id = defaultValues.string(forKey: "userId")
let url = URL(string: "http://localhost:8000/api/store/favorite?")
var request = URLRequest(url: url!)
// POSTを指定
request.httpMethod = "POST"
// POSTするデータをBodyとして設定
let postParameters = "user_id=" + user_id! + "&store_id=" + store_id
request.httpBody = postParameters.data(using: .utf8)
let session = URLSession.shared
session.dataTask(with: request) { (data, response, error) in
if error == nil, let data = data, let response = response as? HTTPURLResponse {
// HTTPヘッダの取得
print("Content-Type: \(response.allHeaderFields["Content-Type"] ?? "")")
// HTTPステータスコード
print("statusCode: \(response.statusCode)")
print(String(data: data, encoding: .utf8) ?? "")
}
}.resume()
}
@IBAction func locationBtn(_ sender: Any) {
let lat = store![0].lat
let lng = store![0].lng
if UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!){
let urlStr : String = "comgooglemaps://?daddr=\(lat),\(lng)&directionsmode=walking&zoom=14"
UIApplication.shared.open(URL(string:urlStr)!,options: [:], completionHandler: nil)
}else{
let daddr = String(format: "%f,%f", lat, lng)
let urlString = "http://maps.apple.com/?daddr=\(daddr)&dirflg=w"
let encodeUrl = urlString.addingPercentEncoding(withAllowedCharacters:NSCharacterSet.urlQueryAllowed)!
let url = URL(string: encodeUrl)!
UIApplication.shared.open(url,options: [:], completionHandler: nil)
}
}
}
请注意,大部分代码并没有解决您的特定问题,而是使C ++ 11/14以可靠的方式工作。我尝试将其保留为与原始示例大致相同的方式,也为了在此处获得完整的工作代码。