在Objective-C中,可以description向其类添加一个方法以帮助调试:
@implementation MyClass
- (NSString *)description
{
return [NSString stringWithFormat:@"<%@: %p, foo = %@>", [self class], foo _foo];
}
@end
Run Code Online (Sandbox Code Playgroud)
然后在调试器中,您可以执行以下操作:
po fooClass
<MyClass: 0x12938004, foo = "bar">
Run Code Online (Sandbox Code Playgroud)
Swift中的等价物是什么?Swift的REPL输出可能会有所帮助:
1> class MyClass { let foo = 42 }
2>
3> let x = MyClass()
x: MyClass = {
foo = 42
}
Run Code Online (Sandbox Code Playgroud)
但我想重写此行为以打印到控制台:
4> println("x = \(x)")
x = C11lldb_expr_07MyClass (has 1 child)
Run Code Online (Sandbox Code Playgroud)
有没有办法清理这个println输出?我见过Printable协议:
/// This protocol should be adopted by types that wish …Run Code Online (Sandbox Code Playgroud) C#具有允许您以字符串格式说明符指定参数索引的语法,例如:
string message = string.Format("Hello, {0}. You are {1} years old. How does it feel to be {1}?", name, age);
Run Code Online (Sandbox Code Playgroud)
您可以多次使用参数,也可以省略使用时提供的参数.另一个问题是以%[index]$[format]例如C/C++的形式提到相同的格式%1$i.我无法让NSString 完全遵守这种语法,因为它在从格式中省略参数时表现得很好.以下内容无法按预期工作(EXC_BAD_ACCESS,因为它尝试将age参数取消引用为NSObject*):
int age = 23;
NSString * name = @"Joe";
NSString * message = [NSString stringWithFormat:@"Age: %2$i", name, age];
Run Code Online (Sandbox Code Playgroud)
只有在格式中没有缺少参数时才会遵守位置参数(这似乎是一个奇怪的要求):
NSString * message = [NSString stringWithFormat:@"Age: %2$i; Name: %1$@", name, age];
Run Code Online (Sandbox Code Playgroud)
所有这些调用在OS X中都能正常工作:
printf("Age: %2$i", [name UTF8String], age);
printf("Age: %2$i %1$s", [name UTF8String], age);
Run Code Online (Sandbox Code Playgroud)
有没有办法在Objective-C/Cocoa中使用NSString来实现这个目的?它对于本地化目的很有用.
我目前正在显示如下的UIViewController:
[[self navigationController] presentModalViewController:modalViewController animated:YES];
Run Code Online (Sandbox Code Playgroud)
并像这样隐藏它:
[self.navigationController dismissModalViewControllerAnimated:YES];
Run Code Online (Sandbox Code Playgroud)
动画"从底部向上滑动"......然后向下滑动.如何更改动画样式?我可以让它淡入/淡出吗?
干杯!
我有一个产品安装程序可执行文件,可以将一些文件复制到用户的硬盘上.它不是正常意义上的典型安装程序(它不会向"开始"菜单或"程序文件"文件夹添加任何内容).
每次在Vista上运行安装程序时,在exe终止后,Vista会生成一个任务对话框:
是否有一个函数我需要从exe或注册表项调用来设置,以指示操作系统正确安装程序(或至少压制此消息)?
相关问题: Windows 7 RC中的"此程序可能未正确安装"消息(适用于Windows 7特定问题)
它们可以如下使用:
FieldInfo field = fieldof(string.Empty);
MethodInfo method1 = methodof(int.ToString);
MethodInfo method2 = methodof(int.ToString(IFormatProvider));
Run Code Online (Sandbox Code Playgroud)
fieldof 可以编译为IL为:
ldtoken <field>
call FieldInfo.GetFieldFromHandle
Run Code Online (Sandbox Code Playgroud)
methodof 可以编译为IL为:
ldtoken <method>
call MethodBase.GetMethodFromHandle
Run Code Online (Sandbox Code Playgroud)
无论何时使用typeof运算符,您都可以获得完美的查找所有引用结果.不幸的是,一旦你去了田野或方法,你最终会遇到令人讨厌的黑客攻击.我想你可以做以下事情......或者你可以回去按名字命名.
public static FieldInfo fieldof<T>(Expression<Func<T>> expression)
{
MemberExpression body = (MemberExpression)expression.Body;
return (FieldInfo)body.Member;
}
public static MethodInfo methodof<T>(Expression<Func<T>> expression)
{
MethodCallExpression body = (MethodCallExpression)expression.Body;
return body.Method;
}
public static MethodInfo methodof(Expression<Action> expression)
{
MethodCallExpression body = (MethodCallExpression)expression.Body;
return body.Method;
}
public static void Test()
{
FieldInfo field = fieldof(() => string.Empty);
MethodInfo method1 …Run Code Online (Sandbox Code Playgroud) 我们发现编译Linq查询比每次编译要快得多,所以我们想开始使用编译查询.问题是它使代码更难阅读,因为查询的实际语法在某些其他文件中是关闭的,远离它的使用位置.
在我看来,有可能编写一个方法(或扩展方法),它使用反射来确定传入的查询,并自动缓存已编译的版本以供将来使用.
var foo = (from f in db.Foo where f.ix == bar select f).Cached();
Run Code Online (Sandbox Code Playgroud)
Cached()必须反映传入的查询对象并确定所选的表和查询的参数类型.显然,反射有点慢,因此使用缓存对象的名称可能会更好(但是第一次编译查询时仍然需要使用反射).
var foo = (from f in db.Foo where f.ix == bar select f).Cached("Foo.ix");
Run Code Online (Sandbox Code Playgroud)
有没有人有这方面的经验,或者知道它是否可能?
更新:对于那些没有看过它的人,可以使用以下代码将LINQ查询编译为SQL:
public static class MyCompiledQueries
{
public static Func<DataContext, int, IQueryable<Foo>> getFoo =
CompiledQuery.Compile(
(DataContext db, int ixFoo) => (from f in db.Foo
where f.ix == ixFoo
select f)
);
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是拥有这些Func<>对象的缓存,我可以在第一次自动编译查询后调用它们.
鉴于变量
string ids = Request.QueryString["ids"]; // "1,2,3,4,5";
Run Code Online (Sandbox Code Playgroud)
有没有办法将它转换为List而不做类似的事情
List<int> myList = new List<int>();
foreach (string id in ids.Split(','))
{
if (int.TryParse(id))
{
myList.Add(Convert.ToInt32(id));
}
}
Run Code Online (Sandbox Code Playgroud) 根据标题.我在Apple网站上搜索过,找不到任何相关信息.我还不是iPhone的开发者成员,所以不能访问那些东西.我只是想开发一个可以与FaceTime协议交谈的Android应用程序.
如果我们所有人都必须向Apple支付费用来查看FaceTime文档以在我们的非iOS应用程序中实现协议,那将会有点愚蠢.
如果我添加一个观察者[NSNotificationCenter defaultCenter],我viewDidLoad应该删除它viewDidUnload吗?
Swift 1.1包含〜>运算符的声明:
infix operator ~> {
associativity left
precedence 255
}
Run Code Online (Sandbox Code Playgroud)
这在Swift中用于什么?它似乎已声明,但没有定义利用它的函数.其他开发人员已经将它用于反应模式和队列之间的编组闭包,但我想知道为什么它在标准框架中定义.我推测它是为了保留开发人员使用的自定义运算符,因为它具有最高的优先级.
c# ×3
iphone ×3
cocoa-touch ×2
objective-c ×2
swift ×2
asp.net-mvc ×1
cocoa ×1
facetime ×1
generics ×1
installer ×1
iqueryable ×1
linq ×1
linq-to-sql ×1
reflection ×1
uac ×1