这是一个相当不错的观点,我希望答案是"开始时并不是一个好主意" - 也就是说,如果某人有点放纵,它有一个我感兴趣的点.
型号代码:
public partial class MyEntities : ObjectContext
{
// the idea is if the object is in a using block, this always gets called?
protected override void Dispose(bool disposing)
{
this.SaveChanges();
base.Dispose(disposing);
}
}
Run Code Online (Sandbox Code Playgroud)
客户代码:
using(var model = new MyEntities())
{
// do something
// no worry about calling model.SaveChanges()
}
Run Code Online (Sandbox Code Playgroud)
我不确定的问题是:
处置正确的地方是因为我出于某种原因想到"终结" - 我总是对C#破坏感到困惑.
在客户端代码中抛出异常的情况下,通常会跳过SaveChanges并且这很好,但是如果我认为这是有效的,那么它总是会调用它.我应该尝试空捕获吗?
public partial class MyEntities : ObjectContext
{
protected override void Dispose(bool disposing)
{
try
{
this.SaveChanges();
}
catch {}
base.Dispose(disposing);
} …Run Code Online (Sandbox Code Playgroud)我有两个nssets.
nsset1: person.id = 1, person.id = 2, person.id = 3
nsset2: person.id = 1, person.id = 2
Run Code Online (Sandbox Code Playgroud)
结果应该是:
nsset1 - nsset2: person (with id 3)
nsset2 - nsset1: null
Run Code Online (Sandbox Code Playgroud)
这两个集中具有相同id的对象是不同的对象,所以我不能简单地做minusSet.
我想做的事情如下:
nsset1: person.id = 1, person.id = 2, person.id = 3
nsset2: person.id = 4, person.id = 5
Run Code Online (Sandbox Code Playgroud)
结果应该是这样的:
nsset1 - nsset2: person (id 1), person (id 2), person (id 3)
nsset2 - nsset1: person (id 4), person (id 5)
Run Code Online (Sandbox Code Playgroud)
做这个的最好方式是什么?
我有这段代码用于从中读取数据AVAssetReaderOutput,该方法在iOS 4.0中运行良好,但是在5.0中它以最糟糕的访问方式崩溃,不知道为什么,任何人都有任何输入?
AVAssetReaderOutput *output=[myOutputs objectAtIndex:0];
int totalBuff=0;
while(TRUE)
{
CMSampleBufferRef ref=[output copyNextSampleBuffer];
if(ref==NULL)
break;
//copy data to file
//read next one
AudioBufferList audioBufferList;
NSMutableData *data=[[NSMutableData alloc] init];
CMBlockBufferRef blockBuffer;
CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(ref, NULL, &audioBufferList, sizeof(audioBufferList), NULL, NULL, 0, &blockBuffer);
for( int y=0; y<audioBufferList.mNumberBuffers; y++ )
{
AudioBuffer audioBuffer = audioBufferList.mBuffers[y];
Float32 *frame = audioBuffer.mData;
NSLog(@"Gonna write %d", audioBuffer.mDataByteSize);
//crashes here
[data appendBytes:frame length:audioBuffer.mDataByteSize];
}
totalBuff++;
CFRelease(blockBuffer);
CFRelease(ref);
[fileHandle writeData:data];
[data release];
}
Run Code Online (Sandbox Code Playgroud)
谢谢
丹尼尔
我需要格式化一个asp.net文本框,使它只接受用户输入的形式
### - ### - ### (#:仅限数字).
建议或答案请.
我想减去两个gettimeofday实例,并以毫秒为单位显示答案.
这个想法是:
static struct timeval tv;
gettimeofday(&tv, NULL);
static struct timeval tv2;
gettimeofday(&tv2, NULL);
static struct timeval tv3=tv2-tv;
Run Code Online (Sandbox Code Playgroud)
然后将'tv3'转换为毫秒分辨率.
我有兴趣学习如何制作自己的操作系统.
我知道这不是一夜之间发生的事情.
我有几个问题:
我正在尝试使用boost格式化字符串:
wchar_t *msg;
// fill msg
boost::format("Error: %s") % msg).str()
Run Code Online (Sandbox Code Playgroud)
我得到的不是msg的内容,是以十六进制表示的msg的地址.
这些事情没有成功:
boost::format("Error: %s") % new std::wstring(msg)
boost::format("Error: %1%") % msg
Run Code Online (Sandbox Code Playgroud)
注意:即使我认为它无关紧要,但我填写msg的方式是:
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | 40, NULL, GetLastError(), MAKELANGID(0, SUBLANG_ENGLISH_US), (LPTSTR) &msg, 512, NULL);
Run Code Online (Sandbox Code Playgroud)
和Visual Studio Watch正确显示msg的内容.
我需要将一些代码从FreePascal移植到C.我是一名专业的C开发人员但对Pascal一无所知.大多数代码都可以轻松移植,但是一行代表让我很头疼.究竟应该做什么:
New(newBack);
curBackPtr^ := newBack;
curBackPtr := @(newBack^.next);
Run Code Online (Sandbox Code Playgroud)
让我感到困惑的是,在没有被访问的情况下newBack分配给它curBackPtr并在其之后分配的事实.那么第一个任务是不是多余的,可以安全地删除?或者我在这里遗漏了什么?newBack.nextcurBackPtrcurBackPtr
有没有办法定义如何使用Resharper(6)"清理"注释?我没有找到解决这个问题的方法.
在代码清理之前:
/// <summary>
/// This is a comment.
/// </summary>
public class MyClass
{
... <not yet cleaned code is here>
}
Run Code Online (Sandbox Code Playgroud)
代码清理后:
/// <summary>
/// This is a comment.
/// </summary>
public class MyClass
{
... <cleaned code is here>
}
Run Code Online (Sandbox Code Playgroud)
通缉结果:
/// <summary>
/// This is a comment.
/// </summary>
public class MyClass
{
... <cleaned code is here>
}
Run Code Online (Sandbox Code Playgroud)
备注:我不想为单个类等禁用清理.我只想更改意外添加的注释中的缩进.
该程序将输出设为5.但是在替换所有宏之后,它将导致--5.这应该导致编译错误,试图减少5.但它编译并运行良好.
#include <stdio.h>
#define A -B
#define B -C
#define C 5
int main()
{
printf("The value of A is %d\n", A);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么没有错误?
c# ×3
c++ ×2
ios ×2
iphone ×2
asp.net ×1
audio ×1
avfoundation ×1
boost ×1
c ×1
finalizer ×1
formatting ×1
freepascal ×1
gettimeofday ×1
ios5 ×1
ipad ×1
linux ×1
macros ×1
nsset ×1
objective-c ×1
pascal ×1
resharper ×1
validation ×1
wchar-t ×1