小编Joe*_*e H的帖子

无法让文字转语音起作用

我在尝试下面的代码时收到以下消息:

2014-07-28 13:19:14.251 MySingleView [3750:461865]语音初始化错误:2147483665

我做错了什么,或者这是一个错误?

我在Mac上使用Xcode6-Beta 4运行Mavericks的iPad 2模拟器中运行此功能.

import UIKit
import AVFoundation

class ViewController: UIViewController {
    var voice = AVSpeechSynthesizer()

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    @IBAction func buttonPushed(sender: UIButton) {
        var utterance = AVSpeechUtterance(string:"This is a test")
        voice.speakUtterance(utterance)
    }
}
Run Code Online (Sandbox Code Playgroud)

swift

18
推荐指数
1
解决办法
5142
查看次数

这是一个在C#中创建线程安全类的好设计吗?

通常,当我想要一个线程安全的类时,我会执行以下操作:

public class ThreadSafeClass
{
    private readonly object theLock = new object();

    private double propertyA;
    public double PropertyA
    {
        get
        {
            lock (theLock)
            {
                return propertyA;
            }
        }
        set
        {
            lock (theLock)
            {
                propertyA = value;
            }
        }
    }

    private double propertyB;
    public double PropertyB
    {
        get
        {
            lock (theLock)
            {
                return propertyB;
            }
        }
        set
        {
            lock (theLock)
            {
                propertyB = value;
            }
        }
    }

    public void SomeMethod()
    {
        lock (theLock)
        {
            PropertyA = 2.0 * PropertyB;
        }
    } …
Run Code Online (Sandbox Code Playgroud)

.net c# multithreading thread-safety

12
推荐指数
1
解决办法
1992
查看次数

静态构造函数可以在非静态构造函数之后运行.这是编译器错误吗?

以下程序的输出是:

Non-Static
Static
Non-Static
Run Code Online (Sandbox Code Playgroud)

这是编译器错误吗?我期望:

Static
Non-Static
Non-Static
Run Code Online (Sandbox Code Playgroud)

因为我认为静态构造函数总是在非静态构造函数之前被调用.

我使用.net 3.5和.net 4.0对Visual Studio 2010进行了测试.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StaticConstructorBug
{
    class Program
    {
        static void Main(string[] args)
        {
            var mc = new MyClass();

            Console.ReadKey();
        }
    }

    public class MyClass
    {
        public MyClass()
        {
            Console.WriteLine("Non-static");
        }

        static MyClass()
        {
            Console.WriteLine("Static");
        }

        public static MyClass aVar = new MyClass();
    }
}
Run Code Online (Sandbox Code Playgroud)

c# static-constructor

11
推荐指数
2
解决办法
4027
查看次数

我必须在C#中哈希两次吗?

我有以下代码:

class MyClass
{
    string Name;
    int NewInfo;
}

List<MyClass> newInfo = .... // initialize list with some values
Dictionary<string, int> myDict = .... // initialize dictionary with some values

foreach(var item in newInfo)
{
    if(myDict.ContainsKey(item.Name)) // 'A' I hash the first time here
         myDict[item.Name] += item.NewInfo // 'B' I hash the second (and third?) time here
    else
        myDict.Add(item.Name, item.NewInfo);
}
Run Code Online (Sandbox Code Playgroud)

有没有办法避免在字典中进行两次查找 - 第一次查看是否包含条目,第二次更新值?在'B'行上甚至可能有两个哈希查找 - 一个用于获取int值,另一个用于更新它.

c#

5
推荐指数
1
解决办法
438
查看次数

DataGridView - 同时输入和输出 - 这是DataGridView中的错误

我创建了一个C#Windows Forms应用程序,我试图尽可能简单地演示我遇到的问题.我正在尝试使用DataGridView允许用户在一列中输入,同时从后台线程获取另一列中的更新.

问题是输入列实际上是不可编辑的,因为 - 我认为 - 用于输出列的更新会导致输入列在用户尝试更改时使用其当前值进行更新.

这是DataGridView中的错误吗?有没有更好的方法来做这种事情?有人可以推荐一个好的解决方法吗?

以下代码演示了此问题."输出"列将不断更新,"输入"列几乎不可编辑.我已将设计器代码(Form1.designer.cs)和Main(来自Program.cs)合并到表单代码(Form1.cs)中 - 因此以下代码应该自行运行.

using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Timers;

public partial class Form1 : Form
{
    private System.ComponentModel.IContainer components = null;

    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void …
Run Code Online (Sandbox Code Playgroud)

.net c# datagridview winforms

5
推荐指数
1
解决办法
1325
查看次数

将实时数据存储到1000个文件中

我有一个程序,可以接收1000个主题的实时数据.它平均每秒收到5000条消息.每条消息由两个字符串,一个主题和一个消息值组成.我想保存这些字符串以及指示消息到达时间的时间戳.

我在'Core 2'硬件上使用32位Windows XP并在C#中编程.

我想将这些数据保存到1000个文件中 - 每个主题一个.我知道很多人会想告诉我将数据保存到数据库中,但我不想走这条路.

我考虑过几种方法:

1)打开1000个文件,并在数据到达时写入每个文件.我对此有两个担忧.我不知道是否可以同时打开1000个文件,我不知道这会对磁盘碎片产生什么影响.

2)写入一个文件 - 以某种方式 - 稍后处理它以生成1000个文件.

3)将其全部保存在RAM中直到当天结束,然后一次写入一个文件.我认为如果我有足够的RAM,这将很有效,尽管我可能需要移动到64位以超过2 GB的限制.

你会如何解决这个问题?

c# real-time

3
推荐指数
1
解决办法
1278
查看次数