我一直在使用一个小类来模拟一些Python项目中的Enums.有没有更好的方法,或者这对某些情况最有意义?
这里的类代码:
class Enum(object):
'''Simple Enum Class
Example Usage:
>>> codes = Enum('FOO BAR BAZ') # codes.BAZ will be 2 and so on ...'''
def __init__(self, names):
for number, name in enumerate(names.split()):
setattr(self, name, number)
Run Code Online (Sandbox Code Playgroud) 我有一个自定义UIView,它生成一组子视图,并以像tile这样的行和列显示它们.我想要实现的是允许用户触摸屏幕,当手指移动时,其下方的瓷砖消失.
下面的代码是包含切片的自定义UIView:
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
int i, j;
int maxCol = floor(self.frame.size.width/TILE_SPACING);
int maxRow = floor(self.frame.size.height/TILE_SPACING);
CGRect frame = CGRectMake(0, 0, TILE_WIDTH, TILE_HEIGHT);
UIView *tile;
for (i = 0; i<maxCol; i++) {
for (j = 0; j < maxRow; j++) {
frame.origin.x = i * (TILE_SPACING) + TILE_PADDING;
frame.origin.y = j * (TILE_SPACING) + TILE_PADDING;
tile = [[UIView alloc] initWithFrame:frame];
[self addSubview:tile];
[tile release];
}
}
}
return self;
}
- (void)touchesBegan: (NSSet *)touches …Run Code Online (Sandbox Code Playgroud) 我需要将一个像"一.二.三.四.五.六.七.八.九.十一.十一"的字符串匹配成四个句子的组.我需要一个正则表达式,在每四个句点后将字符串分成一个组.就像是:
string regex = @"(.*.\s){4}";
System.Text.RegularExpressions.Regex exp = new System.Text.RegularExpressions.Regex(regex);
string result = exp.Replace(toTest, ".\n");
Run Code Online (Sandbox Code Playgroud)
不起作用,因为它将替换句点之前的文本,而不仅仅是句点本身.我如何只统计句点并用句点和换行符替换它们?
我有一个包含NSBox控件的窗口.在那个NSBox是几个其他控件,(弹出窗口,文本字段等...)
我在同一个NIB文件中有另外两个NSBox,这些文件中充满了我想在特定条件下与第一个交换的控件.我希望这会发生一个很好的交叉淡入淡出效果,所以我做了以下几点:
在NSWindowController的-awakeFromNib方法中:
[[self.myWindow contentView] setWantsLayer:YES];
Run Code Online (Sandbox Code Playgroud)
在我正在切换视图的方法中,我使用以下代码:
[[[self.myWindow contentView] animator] replaceSubview:previousView with:newView];
Run Code Online (Sandbox Code Playgroud)
这很好,视图交叉淡出就像我期望的那样.问题是对视图的控制有时会消失,没有明显的原因.它并不总是相同的视图(尽管NSPopUpButtons似乎特别容易出现这种情况),并且它们通常在有焦点时重新出现.
我是否还必须支持所有控制层?
更新:在显式分组中包装动画没有任何区别.在Nib文件中打开setWantsLayer也没有什么区别,但有趣的是,当我这样做时,NSPopupbuttons消失,直到点击其包含视图.在NSPopupbuttons上手动设置Layer也没有什么区别.
似乎其他人遇到了这个问题,但我找不到任何解决方案:
http://www.cocoabuilder.com/archive/message/cocoa/2008/3/30/202691 http://www.cocoabuilder.com/archive/message/cocoa/2008/4/25/205134
我需要将NSProgressIndicator中的图像放入NSOutlineView Cell中.我已经编写了代码,为确定的指标执行此操作,它的工作效果非常好:
NSProgressIndicator *progressIndicator = [[NSProgressIndicator alloc] initWithFrame:NSMakeRect(0, 0, 16, 16)];
[progressIndicator setStyle:NSProgressIndicatorSpinningStyle];
[progressIndicator setIndeterminate:NO];
[progressIndicator setMaxValue:100.0];
[progressIndicator setDoubleValue:somePercentage];
NSImage *updateImage = [[NSImage alloc] initWithData:[progressIndicator dataWithPDFInsideRect:[progressIndicator frame]]];
[progressIndicator release];
return [updateImage autorelease];
Run Code Online (Sandbox Code Playgroud)
我试图修改代码也给我不确定的指标图像.然而,对于不确定的情况,我总是得到一个空白的16x16图像.(我已经通过在每种情况下将图像写入文件来确认这一点,确定的情况给出了进度指示器图像,不确定的情况总是16x16白色方块).
修改后的代码是:
if(self.lengthUnknown)
{
NSProgressIndicator *progressIndicator = [[NSProgressIndicator alloc] initWithFrame:NSMakeRect(0, 0, 16, 16)];
[progressIndicator setStyle:NSProgressIndicatorSpinningStyle];
[progressIndicator setIndeterminate:YES];
NSImage *updateImage = [[NSImage alloc] initWithData:[progressIndicator dataWithPDFInsideRect:[progressIndicator frame]]];
[progressIndicator release];
return [updateImage autorelease];
}
else
{
// Same code as the first listing, this case works fine
}
Run Code Online (Sandbox Code Playgroud)
不确定的进度指标是否使用某种类型的绘图导致-dataWithPDFInsideRect:无法捕获其图像?
更多信息:我尝试将进度指示器设置为不使用线程动画以及尝试通过NSImage的lockFocus方法获取内容,如下所示,但这些尝试都没有产生任何影响.
Dave在 …
我正在寻找一种快速检测图片中面部的方法(存储为JPG或任何其他流行的图像格式).任何流行的编程语言中的代码都可以(Python,C#,Java,Matlab等).我也愿意自己实现一个算法,只要它被证明是一个很好的工作.
或者,如果有针对该问题的已知免费软件(最好是开放但不是必需的)工具或SDK,我也会尝试它们.
最后,商业产品也将被考虑,如果一切都失败了,所以也推荐这些产品.
我为我的Mac写了一个廉价而欢快的声板,我用NSSound播放各种声音:
-(void)play:(NSSound *)soundEffect:(BOOL)stopIfPlaying {
BOOL wasPlaying = FALSE;
if([nowPlaying isPlaying]) {
[nowPlaying stop];
wasPlaying = TRUE;
}
if(soundEffect != nowPlaying)
{
[soundEffect play];
nowPlaying = soundEffect;
} else if(soundEffect == nowPlaying && ![nowPlaying isPlaying] && !wasPlaying) {
[nowPlaying play];
}
}
Run Code Online (Sandbox Code Playgroud)
而不是仅仅停止它,我希望它能在几秒钟内消失.
是否值得编写如下代码来复制数组元素:
#include <iostream>
using namespace std;
template<int START, int N>
struct Repeat {
static void copy (int * x, int * y) {
x[START+N-1] = y[START+N-1];
Repeat<START, N-1>::copy(x,y);
}
};
template<int START>
struct Repeat<START, 0> {
static void copy (int * x, int * y) {
x[START] = y[START];
}
};
int main () {
int a[10];
int b[10];
// initialize
for (int i=0; i<=9; i++) {
b[i] = 113 + i;
a[i] = 0;
}
// do the copy …Run Code Online (Sandbox Code Playgroud) 我对进化算法感兴趣.我用R测试了遗传算法,但有人尝试过遗传编程吗?你知道吗,如果有代码用R写的代码
如何在 python 中将 setdefault 用于嵌套字典结构。例如..
self.table[field] = 0
self.table[date] = []
self.table[value] = {}
Run Code Online (Sandbox Code Playgroud)
我想为这些设置默认值。
objective-c ×4
cocoa ×2
python ×2
algorithm ×1
c# ×1
c++ ×1
cocoa-touch ×1
dictionary ×1
enums ×1
iphone ×1
nlp ×1
nssound ×1
r ×1
regex ×1
templates ×1