是否有可能在Swift中捕获异常?给出以下代码:
NSException.raise(NSRangeException,
format: "Now you've gone too far!",
arguments: CVaListPointer(fromUnsafePointer: UnsafePointer()))
Run Code Online (Sandbox Code Playgroud)
是否可以防止异常崩溃整个程序?也就是说,Objective-C中Swift等效的是以下内容:
@try {
[NSException raise:NSRangeException format:@"Now you've gone too far!"];
}
Run Code Online (Sandbox Code Playgroud) 我在Go中构建了一个简单的队列.它使用内部切片来跟踪其元素.通过附加到切片将元素推送到队列中.我想.Pop()通过删除第一个元素来实现elements.
在许多其他语言中,"弹出"列表的第一个元素是一个单行,这使我相信我的实现在下面是草率和冗长.有没有更好的办法?
type Queue struct {
elements []interface{}
}
func (queue *Queue) Push(element interface{}) {
queue.elements = append(queue.elements, element)
}
func (queue *Queue) Pop() interface{} {
element := queue.elements[0]
if len(queue.elements) > 1 {
queue.elements = queue.elements[1:]
} else {
queue.elements = make([]interface{}, 0)
}
return element
}
Run Code Online (Sandbox Code Playgroud)
请注意,我希望Queue如果恐慌len(queue.elements) == 0.我不检查边界不是疏忽.
我想在Django 1.3应用程序中翻译包含URL的段落.
<p>
First <a href="{% url edit-profile username=user.username %}">edit your profile</a>, please.
</p>
Run Code Online (Sandbox Code Playgroud)
根据语言,<a>标签包围的文本肯定会改变.如何让翻译人员决定链接位置?将整个事物包装成a {% trans %}会导致错误:
<p>{% trans "First <a href='{% url edit-profile username=user.username %}'>edit your profile</a>, please." %}</p>
Run Code Online (Sandbox Code Playgroud)
抛出的错误是TemplateSyntaxError: Searching for value. Unexpected end of string in column 64: trans "First <a href='{% url edit-profile username=user.username.
我应该怎么做呢?我是否必须在视图中确定URL,然后将该URL作为字符串传递给模板?对于我认为是一个非常普遍的问题,这似乎是一个非常复杂的解决方案.
部分基于对SO问题的回复中的建议,我尝试用/Developer/Library/Xcode/PrivatePlugIns/IDECodeSnippetLibrary.ideplugin别名替换Dropbox文件夹中的精确副本,但这样做会导致Xcode 4.2因内部逻辑错误而崩溃.
我想在我的工作和家用机器上同步片段.现在我正在使用Alfred作为片段,但让Xcode处理片段,以方便跳跃和智能感知是很好的.有没有人尝试类似的东西?
我是Go的新手,已经实现了二叉搜索树.树可以存储任何值(特别是实现的任何值interface{}).
我想在此实现的基础上构建一个自平衡的红黑树.在面向对象的语言中,我定义了一个子类,BinarySearchTree它添加了一个color数据成员,然后重写该Insert方法来执行平衡操作.
问题:如何在Go中实现二进制搜索树和红黑树而不重复代码?
这是我的二叉搜索树实现:
package trees
import (
"github.com/modocache/cargo/comparators"
"reflect"
)
type BinarySearchTree struct {
Parent *BinarySearchTree
Left *BinarySearchTree
Right *BinarySearchTree
Value interface{} // Can hold any value
less comparators.Less // A comparator function to determine whether
// an inserted value is placed left or right
}
func NewBinarySearchTree(value interface{}, less comparators.Less) *BinarySearchTree {
return &BinarySearchTree{Value: value, less: less}
}
func (tree *BinarySearchTree) Insert(value interface{}) *BinarySearchTree {
if tree.less(value, tree.Value) { …Run Code Online (Sandbox Code Playgroud) 以下接口定义了一组由mooing对象实现的方法:
type Mooing interface {
Moo() string
}
Run Code Online (Sandbox Code Playgroud)
以下定义了一组通过放牧对象实现的方法:
type Grazing interface {
EatGrass()
}
Run Code Online (Sandbox Code Playgroud)
我有一个在奶牛上运作的功能:
func Milk(cow *Cow)
Run Code Online (Sandbox Code Playgroud)
然而,它不一定是一头牛 - 任何符合Mooing并且Grazing足够接近的东西.在Go中,是否可以指定参数Mooing and Grazing?在伪代码中,类似下面的内容?
func Milk(cow {Mooing, Grazing})
Run Code Online (Sandbox Code Playgroud)
换句话说,只接受满足这两个接口的参数.
我有一组Swift和Objective-C文件,它们扩展了XCTest框架中的一些功能.
我很容易创建一个由这些文件组成的Cocoa框架.我将所有文件添加到OS X框架目标中,并且由于其中一些文件导入了XCTest头文件,因此我链接XCTest.framework到了我的框架.用户可以将框架添加到他们的单元测试目标中,前提是他们正在为OS X构建.
问题:如何为构建iOS模拟器的用户执行相同的操作(即:分发此代码)?
我无法创建一个导入的Cocoa Touch框架 - XCTest.framework因此导致以下链接器错误("Quick"是框架的名称):
Ld /Users/modocache/Library/Developer/Xcode/DerivedData/Quick-cajmiuprocunntdhiwsnsfzqydkc/Build/Products/Debug-iphonesimulator/Quick-iOS.framework/Quick-iOS normal x86_64
cd /Users/modocache/GitHub/modocache/Quick
export IPHONEOS_DEPLOYMENT_TARGET=8.0
export PATH="/Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Applications/Xcode6-Beta.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -arch x86_64 -dynamiclib -isysroot /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk -L/Users/modocache/Library/Developer/Xcode/DerivedData/Quick-cajmiuprocunntdhiwsnsfzqydkc/Build/Products/Debug-iphonesimulator -F/Users/modocache/Library/Developer/Xcode/DerivedData/Quick-cajmiuprocunntdhiwsnsfzqydkc/Build/Products/Debug-iphonesimulator -F/Applications/Xcode6-Beta.app/Contents/Developer/Library/Frameworks -filelist /Users/modocache/Library/Developer/Xcode/DerivedData/Quick-cajmiuprocunntdhiwsnsfzqydkc/Build/Intermediates/Quick.build/Debug-iphonesimulator/Quick-iOS.build/Objects-normal/x86_64/Quick-iOS.LinkFileList -install_name @rpath/Quick-iOS.framework/Quick-iOS -Xlinker -rpath -Xlinker @executable_path/Frameworks -Xlinker -rpath -Xlinker @loader_path/Frameworks -Xlinker -objc_abi_version -Xlinker 2 -Xlinker -no_implicit_dylibs -mios-simulator-version-min=8.0 -framework XCTest -single_module -compatibility_version 1 -current_version 1 -Xlinker -dependency_info -Xlinker /Users/modocache/Library/Developer/Xcode/DerivedData/Quick-cajmiuprocunntdhiwsnsfzqydkc/Build/Intermediates/Quick.build/Debug-iphonesimulator/Quick-iOS.build/Objects-normal/x86_64/Quick-iOS_dependency_info.dat -o /Users/modocache/Library/Developer/Xcode/DerivedData/Quick-cajmiuprocunntdhiwsnsfzqydkc/Build/Products/Debug-iphonesimulator/Quick-iOS.framework/Quick-iOS
ld: building for iOS Simulator, but linking against dylib built for MacOSX file '/Applications/Xcode6-Beta.app/Contents/Developer/Library/Frameworks/XCTest.framework/XCTest' …Run Code Online (Sandbox Code Playgroud) 这是我关于SO的第二篇文章,以及我关于Django的第二篇文章 - 请保持温和.
我正在研究一个简单的用户模型.用户应该能够注册该网站,注册后他们应该能够在登录时更改其帐户设置.但是,如果我执行以下操作,用户会看到一个包含我不希望他们编辑的各种信息的巨大表单:
from django.shortcuts import render_to_response, get_object_or_404
from django.core import urlresolvers
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import UserChangeForm
@login_required
def settings( request, template_name = 'accounts/settings.html' ):
"""
Processes requests for the settings page, where users
can edit their profiles.
"""
page_title = 'Account Settings'
if request.method == 'POST':
postdata = request.POST.copy()
form = UserChangeForm( postdata )
if form.is_valid():
form.save()
else:
form = UserChangeForm()
title = 'Settings'
return render_to_response( …Run Code Online (Sandbox Code Playgroud) Xcode最强大的功能之一就是它的Intellisense完成功能,它会在您输入Foundation/Cocoa/UIKit API的名称时显示潜在候选项列表.我对MacRuby,PyObjC或更新的RubyMotion非常感兴趣,但是如果没有代码完成,这些工具似乎比它们的价值更麻烦.
上述三种技术中的任何一种,对于任何文本编辑器(但最好是Vim或Xcode),是否有任何代码完成功能(不一定是Intellisense )?IDE解决方案的奖励点,允许在单个命令中构建和运行应用程序(如Xcode的"运行"按钮).
据我所知,Xcode 4放弃了对MacRuby/PyObjC的支持,因此Intellisense不再可用.为了完成代码,我应该在Xcode 4旁边安装Xcode 3吗?其他人如何做到这一点(当然你们使用某种形式的代码完成 - 我不相信任何人都能记住Foundation/Cocoa/UIKit中的所有类)?
我想通过以下方式通过 CMake 添加测试:
ADD_UNIT_TEST(MyTest)
set_tests_properties(
MyTest
PROPERTIES
PASS_REGULAR_EXPRESSION
"This matches the first line\n"
"This matches the second line"
)
Run Code Online (Sandbox Code Playgroud)
这可能吗?如何?
我views.py变得非常笨拙,所以我想将它分成views我应用程序内部的单独模块.但是,我不确定这是一个好主意,原因有两个:
如果我的视图文件与应用程序名称相同,我无法在不使用的情况下导入模型django.db.get_model,因此我担心我的方法可能存在缺陷.我听说最好避免模块内的名称冲突; 我应该重命名我的视图文件?
我不确定创建一个views模块是否被认为是Django社区中的良好实践.
例如,对于名为app的应用程序blogs,使用Blog模型和Post模型:
blogs/
__init__.py
models.py
urls.py
views/
__init__.py
blogs.py
posts.py
Run Code Online (Sandbox Code Playgroud)
这是我的blogs.views.blogs:
# project/blogs/views/blogs.py
from django.db.models import get_model
from django.shortcuts import get_object_or_404
from django.views.generic import ListView, DetailView
# Cannot import model directly, results in `ImportError: No module named models`.
# This can be resolved if I resolve the name collision between this file and
# the app itself.
#
# …Run Code Online (Sandbox Code Playgroud) django ×3
go ×3
xcode ×3
swift ×2
cmake ×1
django-admin ×1
django-forms ×1
django-views ×1
dry ×1
exception ×1
interface ×1
ios ×1
macruby ×1
objective-c ×1
pyobjc ×1
python ×1
queue ×1
regex ×1
rubymotion ×1
try-catch ×1
xcode4 ×1