小编Pie*_*ter的帖子

小于或大于Swift switch语句

我熟悉switchSwift中的语句,但想知道如何用以下代码替换这段代码switch:

if someVar < 0 {
    // do something
} else if someVar == 0 {
    // do something else
} else if someVar > 0 {
    // etc
}
Run Code Online (Sandbox Code Playgroud)

switch-statement swift

130
推荐指数
8
解决办法
5万
查看次数

iOS标签不会在Swift中使用函数更新文本

这个看似简单的问题让我发疯了......我正在玩SwiftyJSON来获取远程数据,这里是我在Swift中的ViewController类的一个片段:

override func viewDidLoad() {
    super.viewDidLoad()

    self.statusLabel.text = "welcome"

    RemoteDataManager.getStatusUpdateFromURL { (statusData) -> Void in
        let json = JSON(data: statusData)
        self.statusLabel.text = "this does not work"
        self.statusLabel.text = self.getMostRecentStatusUpdate(json) // also does not work
    }

}
Run Code Online (Sandbox Code Playgroud)

statusLabel文本设置为"welcome",但之后不会更改.不过好笑的是,什么我把里面的func getMostRecentStatusUpdate(_:)println()被打印到控制台正确,即使它来自远程JSON源(即我知道,此功能).我的问题是我无法将文本打印到UILabel而不是控制台.我没有收到任何错误消息.

我还不熟悉类似的Swift函数MyClass.myMethod { (myData) -> Void in .... },我不明白这里出了什么问题.有任何想法吗?

uiviewcontroller uilabel ios swift

6
推荐指数
1
解决办法
2万
查看次数

从自定义类调用presentViewController

我读了这篇文章这一个关于如何调用presentViewController形式的UIViewController子类之外.在我的例子中,自定义类是NSObject的子类.以下方法是唯一有效的方法(从我读过的例子):

UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

我的问题:是否有一个更好的解决方案,不依赖于appDelegate(因为我知道这种方法在设计方面不是很整洁)......

ios swift uialertcontroller

4
推荐指数
1
解决办法
5619
查看次数

vector <int> - 缺少类型说明符

这是我在Visual C++ Express 2010中处理的类的标题:

/* custom class header to communicate with LynxMotion robot arm */

#include <vector>
using namespace System;
using namespace System::IO::Ports;

public ref class LynxRobotArm
{
public:
    LynxRobotArm();
    ~LynxRobotArm();
    void connectToSerialPort(String^ portName, int baudRate);
    void disconnectFromSerialPort();
    void setCurrentPosition(int channel, int position);
    int getCurrentPosition(int channel);
    void moveToPosition(int channel, int position);

private:
    void initConnection();
    SerialPort^ serialPort;
    array<String^> ^serialPortNames;
    String^ portName;
    int baudRate;
    vector<int> currentPosition;
};
Run Code Online (Sandbox Code Playgroud)

一切正常,直到我改变了最后一行int currentPositionvector<int> currentPosition.如果我现在尝试编译/调试,我会收到以下错误消息:

error C2143: syntax error : missing ';' before …
Run Code Online (Sandbox Code Playgroud)

c++-cli vector visual-c++

3
推荐指数
1
解决办法
3500
查看次数

在Jupyter Python笔记本中清除MatPlotLib图

我希望MatPlotLib中的3D散点图能够在Jupyter Python笔记本中以交互方式旋转.出于这个原因,我集成了一个滑块ipywidgets来更新视角.下面的测试代码显示了我想要实现的目标.问题是在前一个数字下方添加了一个新数字,而不是清除当前数字.我试过plt.close(fig),plt.cla()plt.clf()没有成功.(我进一步意识到重建图形和轴有开销,但这是我目前关注的较小部分...)

这是(测试)代码:

# init
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import ipywidgets as widgets
from IPython.display import display

# generate test data
x = np.random.rand(100)
y = np.random.rand(100)
z = np.random.rand(100)

# prepare plot
def draw_plot(angle1 = 20, angle2 = 40):
    # create figure
    fig = plt.figure(figsize=(15,10))
    ax = fig.add_subplot(111, projection='3d')
    ax.set_xlabel('X axis')
    ax.set_ylabel('Y axis')
    ax.set_zlabel('Z axis')
    ax.scatter(x, y, z)

    # set view angle
    ax.view_init(angle1, …
Run Code Online (Sandbox Code Playgroud)

python matplotlib jupyter-notebook ipywidgets

3
推荐指数
1
解决办法
5839
查看次数

Swift UISplitViewController 具有用于详细信息视图的多个故事板

我几天来一直在为这个问题而苦苦挣扎,无法让它发挥作用。阅读多个 SO 帖子(thisthisthisthisthisthis)。问了我的朋友谷歌,帮助理解了问题,但还没有解决。

在我的 iOS 应用程序的第一个版本(Swift2 和 XCode7)中,我使用 UITableViewController 和 UINavigationViewController 从不同的 UIStoryboard 加载新内容。这一切正常。对于这个应用程序的第二个版本,我想实现一个 UISplitViewController,现在我收到了错误:

*** 由于未捕获的异常“UIViewControllerHierarchyInconsistency”而终止应用程序,原因:“将根视图控制器添加为视图控制器的子级:

我理解提示,但无法以纵向和横向模式下的方式解决它。此外,我的解决方案没有在横向使用 UINavigationController。

我创建了一个测试项目来追踪这个错误,希望能解决它。

这是产生错误的代码:

let storyboard = UIStoryboard(name: "ThirdScreen", bundle: nil)
    let controller = storyboard.instantiateInitialViewController() as! ThirdDetailViewController
    controller.title = "Miracle!"
    splitViewController?.showDetailViewController(controller, sender: nil)
Run Code Online (Sandbox Code Playgroud)

此测试项目的完整代码可从 GitHub 获得

我怎样才能使这项工作?

非常感谢!

xcode uisplitviewcontroller swift

2
推荐指数
1
解决办法
2373
查看次数