小编idm*_*ean的帖子

Swift使用NSStatusBar statusItemWithLength和NSVariableStatusItemLength

我正在尝试从Swift中的状态栏编程主题重写以下代码.

NSStatusBar *bar = [NSStatusBar systemStatusBar];

theItem = [bar statusItemWithLength:NSVariableStatusItemLength];
[theItem retain];

[theItem setTitle: NSLocalizedString(@"Tablet",@"")];
...
Run Code Online (Sandbox Code Playgroud)

我的Swift代码到目前为止:

let bar = NSStatusBar.systemStatusBar()

let sm = bar.statusItemWithLength(NSVariableStatusItemLength)
sm.title = "Tablet"
...
Run Code Online (Sandbox Code Playgroud)

问题是statusItemWithLengthSwift 中的方法除外,CGFloat但在Swift NSVariableStatusItemLength中定义CInt.我看到以下错误:

'CInt' is not convertible to 'CGFloat'
Run Code Online (Sandbox Code Playgroud)

Xcode中的定义:

var NSVariableStatusItemLength: CInt { get }
var NSSquareStatusItemLength: CInt { get }

class NSStatusBar : NSObject {

    class func systemStatusBar() -> NSStatusBar!

    func statusItemWithLength(length: CGFloat) -> NSStatusItem!
    ...
}
Run Code Online (Sandbox Code Playgroud)

难道我做错了什么?我怎样才能解决这个问题?

cocoa swift

11
推荐指数
1
解决办法
4185
查看次数

JavaScript中的可选链接

我最近在Swift编程了很多.今天,当我遇到问题时,我在JavaScipt做了一些工作:

是否有类似于JavaScript中的可选链接?一种防止undefined is not an object没有任何变量的方法?

例:

function test(){
   if(new Date() % 2){
      return {value: function(){/*code*/}};
   }
} 

test().value();
Run Code Online (Sandbox Code Playgroud)

将失败一半的时间因为有时会test返回undefined.

我能想到的唯一解决方案是功能:

function oc(object, key){
   if(object){
      return object[key]();
   }
}

oc(test(), 'value');
Run Code Online (Sandbox Code Playgroud)

我希望能够做到这样的事情:

test()?.value()
Run Code Online (Sandbox Code Playgroud)

问号后面的部分仅在test返回对象时执行.

但这不是很重要.还有更好的东西吗?运营商的神奇组合?

编辑我知道我可以重写test以返回一些东西.但我想知道是否有类似可选链接的东西.我对上面例子的特定解决方案不感兴趣.如果无法控制返回undefined的函数,我也可以使用的东西.

javascript

11
推荐指数
3
解决办法
2591
查看次数

将多个Coffeescript文件合并到一个文件中?(多个子目录)

我有一堆.coffee文件,我需要加入一个文件.

我有像rails应用程序设置的文件夹:

/src/controller/log_controller.coffee
/src/model/log.coffee
/src/views/logs/new.coffee
Run Code Online (Sandbox Code Playgroud)

Coffeescript有一个命令,允许您将多个coffeescripts连接到一个文件中,但它似乎只能用于一个目录.例如,这工作正常:

coffee --output app/controllers.js --join --compile src/controllers/*.coffee
Run Code Online (Sandbox Code Playgroud)

但我需要能够包含一堆类似于这个非工作命令的子目录:

coffee --output app/all.js --join --compile src/*/*.coffee
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?是否有UNIX方法传递子目录中的所有文件的列表?

我在OSX中使用终端.

它们都必须加在一个文件中,因为否则每个单独的文件都会被编译和包装:

(function() { }).call(this);
Run Code Online (Sandbox Code Playgroud)

这打破了一些函数调用的范围.

terminal coffeescript

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

Codeign在10.12 Sierra失败了

刚刚安装10.12和Xcode 8 codesign命令甚至从终端失败A cryptographic verification failure has occurred.,没有进一步的细节.有解决方案吗

macos codesign macos-sierra

10
推荐指数
1
解决办法
3111
查看次数

实现良好的"itoa()"功能的正确方法是什么?

我想知道我执行"itoa"功能是否正确.也许你可以帮我把它变得更"正确",我很确定我错过了什么.(也许已经有一个库按我想要的方式进行转换,但是......找不到任何东西)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

char * itoa(int i) {
  char * res = malloc(8*sizeof(int));
  sprintf(res, "%d", i);
  return res;
}

int main(int argc, char *argv[]) {
 ...
Run Code Online (Sandbox Code Playgroud)

c string char

9
推荐指数
3
解决办法
4万
查看次数

最后是C链表插入节点

我在C中的链表的插入方法遇到了一些麻烦.它似乎只在列表的开头添加.我做的任何其他插入都失败了.而这个CodeBlocks调试器很难理解我仍然没有得到它.它永远不会给我价值,只有内存中的地址.无论如何这是我的功能.你有没有看到它失败的原因?

/* function to add a new node at the end of the list */
int addNodeBottom(int val, node *head){

    //create new node
    node *newNode = (node*)malloc(sizeof(node));

    if(newNode == NULL){
        fprintf(stderr, "Unable to allocate memory for new node\n");
        exit(-1);
    }

    newNode->value = val;

    //check for first insertion
    if(head->next == NULL){
        head->next = newNode;
        printf("added at beginning\n");
    }

    else
    {
        //else loop through the list and find the last
        //node, insert next to it
        node *current = head;
        while(current->next != NULL) …
Run Code Online (Sandbox Code Playgroud)

c binary-tree linked-list insert

9
推荐指数
1
解决办法
13万
查看次数

如何使用pthread_mutex_trylock?

使用trylock:

FILE           *fp;
pthread_mutex_t demoMutex;

void * printHello (void* threadId)
{
    pthread_mutex_trylock (&demoMutex);

    pthread_t      writeToFile = pthread_self ();
    unsigned short iterate;
    for (iterate = 0; iterate < 10000; iterate++)
    {
        fprintf (fp, " %d ",  iterate,         4);
        fprintf (fp, " %lu ", writeToFile, sizeof (pthread_t));
        fprintf (fp, "\n",     writeToFile, 1);
    }

    pthread_mutex_unlock (&demoMutex);
    pthread_exit (NULL);
}
Run Code Online (Sandbox Code Playgroud)

然后是main():

int main ()
{
    pthread_t        arrayOfThreadId [5];
    int                  returnValue;
    unsigned int iterate;

    fp = fopen ("xyz", "w");
    pthread_mutex_init (&demoMutex, NULL);

    for (iterate = …
Run Code Online (Sandbox Code Playgroud)

c mutex pthreads

9
推荐指数
3
解决办法
5万
查看次数

将一个CSS类添加到date_select

我有以下date_select助手.我想添加一个类,但它不生成HTML.

<%= date_select :recipient, :birthday, :order => [:day, :month, :year], :start_year => 1920, :end_year => 2013, :class => "input-mini" %>
Run Code Online (Sandbox Code Playgroud)

我也尝试使用哈希,因为一些解决方案建议但我得到一个语法错误:

<%= date_select :recipient, :birthday, :order => [:day, :month, :year], :start_year => 1920, :end_year => 2013, {:class => "input-mini"} %>
Run Code Online (Sandbox Code Playgroud)

不确定我真的了解何时以及如何使用哈希格式化它.

css ruby-on-rails

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

如何解密简单的XOR加密

我在互联网上发现了以下XOR加密功能:

void xor_encrypt(char *key, char *string)
{
    int i, string_length = strlen(string);
    for(i=0; i<string_length; i++)
    {
        string[i]=string[i]^key[i];
        printf("%i", string[i]);
    }
}
Run Code Online (Sandbox Code Playgroud)

它工作得很完美,但我也想解密字符串.

例如:

void xor_decrypt(char *key, char *encrypted_string)
{
    //decrypt method goes here
}
Run Code Online (Sandbox Code Playgroud)

所以基本上我加密字符串后,我会使用相同的加密密钥来解密以前加密的字符串.

我对编程很新,我只想知道如何解密以前加密的字符串.谢谢,所有的帮助表示赞赏.

c encryption xor

9
推荐指数
2
解决办法
5万
查看次数

Swift - 将数组写入文本文件

我从包含几千行纯文本的文件中读入myArray(本机Swift).

myData = String.stringWithContentsOfFile(myPath, encoding: NSUTF8StringEncoding, error: nil)
var myArray = myData.componentsSeparatedByString("\n")
Run Code Online (Sandbox Code Playgroud)

我更改了myArray中的一些文本(没有点粘贴任何此代码).

现在我想将myArray的更新内容写入新文件.我试过这个..

let myArray2 = myArray as NSArray
myArray2.writeToFile(myPath, atomically: false)
Run Code Online (Sandbox Code Playgroud)

但文件内容则采用plist格式.

有没有办法在Swift(或桥接Swift)中将文本字符串数组写入文件(或循环遍历数组并将每个数组项追加到文件中)?

arrays text file output swift

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