我试图在Swift中初始化空数组.
对于一个字符串数组,它非常简单:
var myStringArray: String[] = []
myStringArray += "a"
myStringArray += "b"
-> ["a", "b"]
Run Code Online (Sandbox Code Playgroud)
对于整数
var myIntArray: Int[] = []
myIntArray += 1
myIntArray += 2
-> [1, 2]
Run Code Online (Sandbox Code Playgroud)
它也适用于其他类型的对象,如NSImage对象:
let path = "/Library/Application Support/Apple/iChat Icons/Flags/"
let image1 = NSImage(byReferencingFile: path + "Brazil.png")
let image2 = NSImage(byReferencingFile: path + "Chile.png")
var myImageArray: NSImage[] = []
myImageArray += image1
myImageArray += image2
-> [<NSImage 0x7fe371c199f0 ...>, <NSImage 0x7fe371f39ea0 ...>]
Run Code Online (Sandbox Code Playgroud)
但是我无法弄清楚初始化一个空字典数组的语法.
我知道你可以有一个字典数组,因为用初始值初始化起作用:
let myDict1 = ["someKey":"someValue"]
let myDict2 = ["anotherKey":"anotherValue"] …
Run Code Online (Sandbox Code Playgroud) 是否有一个标志可以添加到ln
命令中以强制创建目标目录结构(如mkdir -p
).
考虑:
ln -s /Applications/'Sublime Text.app'/Contents/SharedSupport/bin/subl /usr/local/bin/
Run Code Online (Sandbox Code Playgroud)
这为Sublime Text命令行工具添加了一个符号链接.但是如果/ usr/local/bin /不存在则失败.
我试过-f
'强制'标志,但这没有用.
你是否需要测试wether/usr/local/bin/exists并在运行ln
命令之前创建它,或者是否有更高级的方法来创建它?
我正在尝试将一些旧的Applescript移植到新的JavaScript语法中.
事情似乎非常直接,所以:
tell application "System Events" to keystroke "t" using command down
Run Code Online (Sandbox Code Playgroud)
成为:
System = Application('System Events');
System.keystroke("t", {using: "command down"})
Run Code Online (Sandbox Code Playgroud)
但是我不能为我的生活找出如何列出特定位置的文件.在AppleScript中,要返回目录中的文件列表/usr
,您可以:
tell application "System Events" to set fileList to name of items in folder "/usr"
-- > {"bin", "include", "lib", "libexec", "local", "sbin", "share", "standalone", "X11"}
Run Code Online (Sandbox Code Playgroud)
但是我不能为我的生活弄清楚如何在Javascript中做到这一点.
System = Application('System Events')
myPath = Path("/usr")
fileList = System.items(myPath)
-- > message not understood
fileList = System.folder(myPath)
-- > message not understood
fileList = System.diskItems(myPath)
-- > []
fileList …
Run Code Online (Sandbox Code Playgroud) javascript applescript automation osx-yosemite javascript-automation
我目前正在使用Rollup和PostCSS。如何定位我想要处理的特定 scss 文件,而不必使用 @import 'source/scss/main.scss' in my
source/js/entry.js`
例如做类似的事情......
// rollup.config.js
...
postcss({
from: 'source/scss/main.scss'
minimize: true,
sourceMap: true,
extract : 'build/css/main.css'
}),
...
Run Code Online (Sandbox Code Playgroud)
而不是在 source/js/entry.js
//source/js/entry.js
import '../scss/main.scss'; // don't want to import SCSS from within JS file
import { someJSmodule } from './someJSmodule.js';
import { anotherJSmodule } from './anotherJSmodule.js';
Run Code Online (Sandbox Code Playgroud)
这是我的当前 rollup.config.js
// rollup.config.js
import babel from 'rollup-plugin-babel';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import copy from 'rollup-plugin-copy';
import postcss from 'rollup-plugin-postcss';
export …
Run Code Online (Sandbox Code Playgroud) 我正在创建一个BASH脚本,需要安装几个应用程序.ffmpeg
和sox
为确保我的脚本运行时它们就位,我首先检查Homebrew的安装:
#!/bin/bash
which -s brew
if [[ $? != 0 ]] ; then
# Install Homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
fi
Run Code Online (Sandbox Code Playgroud)
然后我检查sox
并ffmpeg
安装:
echo "---- checking for sox ----"
which -s sox || /usr/local/bin/brew install sox
echo "---- checking for ffmpeg ----"
which -s ffmpeg || /usr/local/bin/brew install ffmpeg
Run Code Online (Sandbox Code Playgroud)
我面临的问题是安装Homebrew但是在非标准位置.
我必须使用Homebrew的完整路径,因为这个脚本是在Playtypus中运行的.
所以问题是:如何在BASH脚本中可靠地获取Homebrew的安装路径?
有没有办法http://localhost:3000
在运行时自动启动浏览器并指向它rails server
?
其次,确保这只发生在开发中?
我本以为Launchy会成功,但我正在努力弄清楚把它放在哪里。
我试过添加一个初始化程序 config/initializers/launchy.rb
require 'launchy'
Launchy.open("http://localhost:3000")
Run Code Online (Sandbox Code Playgroud)
这会触发浏览器打开,创建一个新选项卡并按http://localhost:3000
预期访问,但是它在服务器/应用程序完成启动之前运行,并且我在浏览器中收到无法连接到服务器错误。
如果我在浏览器中重新加载我的应用程序就可以正常工作,所以我相信这不是我的应用程序的问题,而是Launch.open
调用的时间问题。
我尝试重命名launchy.rb
为,z_launchy.rb
以便根据文档最后加载它,但仍然存在相同的问题。Launchy 在应用程序准备好之前触发。
我也尝试将代码添加到config/puma.rb
(我使用puma作为我的服务器),config/enviroments/developoment.rb
但总是出现同样的问题。该Launchy.open
命令很快就会被调用。
我应该在哪里调用Launchy.open("http://localhost:3000")
以确保 a) 它在应用程序加载并准备好接收请求后运行;b) 所以它只在开发中运行,而不是在测试或生产中运行?
系统设置 OS X 10.11.3、Rails 4.2.5、ruby 2.2.1p85、puma 2.15.3版
如何垂直对齐两行引导按钮中的图标,使其看起来像:
<button class="btn btn-lg btn-success" style="text-align:left; width:400px">
Line 1 text<br>Line2 text here <span style="vertical-align:middle;" class="glyphicon glyphicon-earphone pull-right"> </span>
</button>
Run Code Online (Sandbox Code Playgroud)
目前我的代码产生这个:
我创建了一个数独拼图创建器/求解器,需要一些 CSS 帮助来设计它的样式。
通常,它们的样式如下:
我正在使用的一些命名。
单元格- 每个单独的单元格包含一个数字。
盒子- 9 个盒子之一,每个盒子包含 3 x 3 个单元格
网格- 整个 9x9 比赛区域。
我的 html 部分是由我的 ruby / Sinatra 应用程序生成的(至少重复的 DIV 是)并且结构如下:
#game {
margin-left: auto;
margin-right: auto;
width: 360px;
}
.cell input {
display: inline-block;
float: left;
width: 40px;
height: 40px;
border-style: solid;
border-width: 1px;
text-align: center;
font-size: 15px;
}
Run Code Online (Sandbox Code Playgroud)
<form action="/" method="post">
<div id="game">
<div class="cell">
<input name="cell[0]" type="text" maxlength="1" value=<%=val%>>
</div>
<div class="cell">
<input name="cell[1]" type="text" maxlength="1" value=<%=val%>> …
Run Code Online (Sandbox Code Playgroud)我试图了解如何最好地将文件从Finder拖放到NSTableView,随后将列出这些文件.
我已经建立了一个小测试应用程序作为试验场.
目前,我有一个NSTableView
与FileListController
它的datasourse.它基本上是一个NSMutableArray File
对象.
我正在尝试找出最佳/正确的方法来实现NSTableView的拖放代码.
我的第一种方法是继承NSTableView并实现所需的方法:
TableViewDropper.h
#import <Cocoa/Cocoa.h>
@interface TableViewDropper : NSTableView
@end
Run Code Online (Sandbox Code Playgroud)
TableViewDropper.m
#import "TableViewDropper.h"
@implementation TableViewDropper {
BOOL highlight;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
// Initialization code here.
NSLog(@"init in initWithCoder in TableViewDropper.h");
[self registerForDraggedTypes:@[NSFilenamesPboardType]];
}
return self;
}
- (BOOL)performDragOperation:(id < NSDraggingInfo >)sender {
NSLog(@"performDragOperation in TableViewDropper.h");
return YES;
}
- (BOOL)prepareForDragOperation:(id)sender {
NSLog(@"prepareForDragOperation called in TableViewDropper.h");
NSPasteboard *pboard = [sender draggingPasteboard]; …
Run Code Online (Sandbox Code Playgroud) 以下Ruby代码让我每个月的第一天:
require 'active_support/all'
# get the date at the beginning of this month
date = Date.today.beginning_of_month
# get the first day of the next 5 months
5.times do |num|
date = date.next_month
p date
end
Run Code Online (Sandbox Code Playgroud)
这使 :
=> Fri, 01 Aug 2014
=> Mon, 01 Sep 2014
=> Wed, 01 Oct 2014
=> Sat, 01 Nov 2014
=> Mon, 01 Dec 2014
Run Code Online (Sandbox Code Playgroud)
但是,如何获得每月的第一个星期四?即
=> Thu, 07 Aug 2014
=> Thu, 04 Sep 2014
=> Thu, 02 Oct 2014
=> Thu, …
Run Code Online (Sandbox Code Playgroud) 鉴于以下ruby代码:
require 'nokogiri'
xml = "<?xml version='1.0' encoding='UTF-8'?>
<ProgramList xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns='http://publisher.webservices.affili.net/'>
<TotalRecords>145</TotalRecords>
<Programs>
<ProgramSummary>
<ProgramID>6540</ProgramID>
<Title>Matalan</Title>
<Limitations>A bit of text
</Limitations>
<URL>http://www.matalan.co.uk</URL>
<ScreenshotURL>http://www.matalan.co.uk/</ScreenshotURL>
<LaunchDate>2009-11-02T00:00:00</LaunchDate>
<Status>1</Status>
</ProgramSummary>
<ProgramSummary>
<ProgramID>11787</ProgramID>
<Title>Club 18-30</Title>
<Limitations/>
<URL>http://www.club18-30.com/</URL>
<ScreenshotURL>http://www.club18-30.com</ScreenshotURL>
<LaunchDate>2013-05-16T00:00:00</LaunchDate>
<Status>1</Status>
</ProgramSummary>
</Programs>
</ProgramList>"
doc = Nokogiri::XML(xml)
p doc.xpath("//Programs")
Run Code Online (Sandbox Code Playgroud)
给出:
=> []
Run Code Online (Sandbox Code Playgroud)
不是预期的.
进一步调查,如果我xmlns='http://publisher.webservices.affili.net/'
从初始<ProgramList>
标签中删除,我得到预期的输出.
事实上,如果我改变xmlns='http://publisher.webservices.affili.net/'
,xmlns:anything='http://publisher.webservices.affili.net/'
我得到预期的输出.
所以我的问题是这里发生了什么?这是格式错误的XML吗?处理它的最佳策略是什么?
虽然在这个例子中它是硬编码的,但XML(将来自)来自Web服务.
我意识到我可以使用这种remove_namespaces!
方法,但Nokogiri文档确实说它是这样"...probably is not a good thing in general"
做的.此外,我对它为什么会发生以及"正确的"XML应该是什么感兴趣.
我有一个使用 AlpineJS 提交的表单。
使用x-init
我想使用从 cookie 读取的两个字母字符串将下拉列表默认为特定国家/地区。
如何使用我输入的 2 个字母代码initForm()
来设置selected
下拉列表的属性?
<div x-data="marketingForm()" x-init="initForm()">
<form id="formID-Contact" name="Contact" data-pageurl="http://localhost:8080/form-test/" class="">
<div>
<label for="country" >Country / Region</label>
<select id="country" name="country" autocomplete="country">
<option value='GB'>United Kingdom</option>
<option value='US'>United States</option>
<option value='CA'>Canada</option>
<option value='MX'>Mexico</option>
</select>
</div>
<a>
<button @click.prevent=submitMarketingForm() id='formSubmitButton'>submit</button>
</a>
</form>
</div>
Run Code Online (Sandbox Code Playgroud)
还有JS
window.marketingForm = () => {
return {
detectedCountry: '',
initForm() {
let country = getCountry() // function that reads a cookie and returns 2 letter code eg. …
Run Code Online (Sandbox Code Playgroud) 考虑这个 bash 脚本:
#!/bin/bash
while true; do
read -p "Give me an answer ? y/n : " yn
case $yn in
[Yy]* ) answer=true ; break;;
[Nn]* ) answer=false ; break;;
* ) echo "Please answer yes or no.";;
esac
done
if $answer
then
echo "Doing something as you answered yes"
else
echo "Not doing anything as you answered no"
fi
Run Code Online (Sandbox Code Playgroud)
从命令行运行时使用:
$ ./script-name.sh
Run Code Online (Sandbox Code Playgroud)
它按预期工作,等待您回答y或n的脚本。
但是,当我上传到 url 并尝试使用以下命令运行它时:
$ curl http://path.to/script-name.sh | bash
Run Code Online (Sandbox Code Playgroud)
我陷入了一个永久循环,脚本说Please answer …
bash ×3
ruby ×3
css ×2
html ×2
javascript ×2
alpine.js ×1
applescript ×1
arrays ×1
automation ×1
cocoa ×1
command-line ×1
curl ×1
dictionary ×1
homebrew ×1
nokogiri ×1
objective-c ×1
osx-yosemite ×1
postcss ×1
puma ×1
rollup ×1
shell ×1
sinatra ×1
swift ×1
terminal ×1
unix ×1
web-services ×1
xml ×1